dcsimg
Sorting on primitive booleans using reflection?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Latta
Posted On:   Monday, March 4, 2002 10:23 AM

I have developed a SortMangager that sorts a list based on the field requested. My problem is that some of the objects contained within the list include get methods that return booleans. What is the most elegant way to use what I have already written in order to perform the sorts? I know that I will need to write a new compare method. Are there any code samples that attempt this sort fo thing? import java.io.*; import java.util.*; import java.lang.reflect.*; import java.lang.*; public class SortManager { public static List sortListFromCache(String pageSetId, String sortOrder, String className, String sortBy) throws InvalidSortException { final String METHODNAM   More>>

I have developed a SortMangager that sorts a list based on the field requested. My problem is that some of the objects contained within the list include get methods that return booleans. What is the most elegant way to use what I have already written in order to perform the sorts? I know that I will need to write a new compare method. Are there any code samples that attempt this sort fo thing?

			
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.lang.*;

public class SortManager {
public static List sortListFromCache(String pageSetId,
String sortOrder,
String className,
String sortBy) throws InvalidSortException {
final String METHODNAME = "sortListFromCache";
List dataListToSort = null;
List dataSortedList = null;

try {
dataListToSort = (List) CacheManager.getCache( pageSetId );
dataSortedList = sortList( dataListToSort, sortOrder, className, sortBy );

} catch (InvalidCacheException e) {
throw new InvalidSortException("com.bac.cpf.sorting.SortManager",
METHODNAME,
"List Not Found.",
e);
}

return dataSortedList;
}
public static List sortList(List dataListToSort,
String sortOrder,
String className,
String sortBy) {
List dataSortedList = null;

dataSortedList = sortAscending(dataListToSort, new DynamicComparator( className, sortBy ));
if (sortOrder.equals("DESCENDING")) {
dataSortedList = sortDescending(dataListToSort);
}

return dataSortedList;
}
private static List sortAscending(List dataListToSort, Comparator c) {
Collections.sort(dataListToSort, c);
return dataListToSort;
}
private static List sortDescending(List dataListToSort) {
Collections.reverse(dataListToSort);
return dataListToSort;
}
}

class DynamicComparator implements Comparator {
public Class cClassDefinition;
public Method mClassMethod;
public Object[] arguments;
public DynamicComparator( String className, String sortBy ) {
try {
cClassDefinition = Class.forName( className );
Class [] parameterTypes = new Class [] {};
mClassMethod = cClassDefinition.getMethod( "get" +
sortBy, parameterTypes );
arguments = new Object [] {};
} catch (Exception e) {
System.out.println("Exception:
" + e);
}
}

public int compare(Object o1,Object o2) {
Comparable cOne = null;
Comparable cTwo = null;
try {
cOne = (Comparable) mClassMethod.invoke( o1 , arguments );
cTwo = (Comparable) mClassMethod.invoke( o2 , arguments );

return ( cOne ).compareTo( cTwo );
} catch (Exception e) {
System.out.println("Exception:
" + e);
}
return 0;
}
}
   <<Less
About | Sitemap | Contact