需求:对List<Map<String, Object>>类型的数据key值为"removeDate",类型为Long进行升序和降序
1、升序
/**
* 升序
* @param list
*/
private static void listSortUp(List<Map<String, Object>> list) {
Collections.sort(list, (o1, o2) -> {
try {
long dt1 = (long) o1.get("removeDate");
long dt2 = (long) o2.get("removeDate");
if (dt1 > dt2) {
return 1;
} else if (dt1 < dt2) {
return -1;
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
});
}
2、降序
/**
* 降序
* @param list
*/
private static void listSortDown(List<Map<String, Object>> list) {
Collections.sort(list, (o1, o2) -> {
try {
long dt1 = (long) o1.get("removeDate");
long dt2 = (long) o2.get("removeDate");
if (dt1 > dt2) {
return -1;
} else if (dt1 < dt2) {
return 1;
} else {
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
});
}
|