private static void ListSort(List<Work_log> list) {
Collections.sort(list, new Comparator<Work_log>() {
@Override
public int compare(Work_log o1, Work_log o2) {
try {
// if (o1.getStart_date().getTime() > o2.getStart_date().getTime()) {
// return -1; //降序
// } else
// if (o1.getStart_date().getTime() < o2.getStart_date().getTime()) {
// return 1;
// } else {
// return 0;
// }
if(o1.getStart_date().after(o2.getStart_date())){
return 1;//升序
}
return -1;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
}
--------------------------工具类
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @param targetList 目标排序List
* @param sortField 排序字段
* @param sortMode 排序方式
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> 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;
}
});
}