-
import java.lang.reflect.Method;
-
import java.util.Collections;
-
import java.util.Comparator;
-
import java.util.List;
-
-
public class SortList<T> {
-
-
-
-
-
-
-
-
-
-
@SuppressWarnings({ "unchecked", "rawtypes" })
-
public void sort(List<T> targetList, final String sortField, final String sortMode) {
-
Collections.sort(targetList, new Comparator() {
-
-
public int compare(Object obj1, Object obj2) {
-
int retVal = 0;
-
try {
-
Method method1 = ((T) obj1).getClass().getMethod(sortField, null);
-
Method method2 = ((T) obj2).getClass().getMethod(sortField, null);
-
if (sortMode != null && "desc".equals(sortMode)) {
-
retVal = method2.invoke(((T) obj2), null).toString()
-
.compareTo(method1.invoke(((T) obj1), null).toString());
-
} else {
-
retVal = method1.invoke(((T) obj1), null).toString()
-
.compareTo(method2.invoke(((T) obj2), null).toString());
-
}
-
} catch (Exception e) {
-
throw new RuntimeException();
-
}
-
return retVal;
-
-
-
-
-
-
}