普通排序
// List<Map<String, Object>> list
list.stream().sorted(Comparator.comparing((Map<String, Object> i) -> i.get("id")).reversed()).collect(Collectors.toList());
// List<Map<String, Object>> list
list.stream().sorted(Comparator.comparing(ClassName::comparingById).reversed()).collect(Collectors.toList());
private static String comparingById(Map<String, Object> map){
return (String) map.get("id");
}
分组排序
- 已知班级所有同学所有学科考试成绩, 根据所有同学的总成绩进行排名
List<String> orderedName = list.stream().collect(Collectors.groupingBy(i -> i.get("name"), Collectors.summingDouble(i -> (double) i.get("score"))))
.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).map(i -> (String) i.getKey())
.collect(Collectors.toList());