/**
* 排序:把不同成绩保存map中,最后实体类根据map的key找到排名 存入实体排名里面,前端取值。
* @return
*/
public static Map<Double,Integer> rankScore(List<DeductPersonVO> stus) {
Map<Double,Integer> map = new HashMap<>();
List<Map.Entry<Double, List<DeductPersonVO>>> list = stus.stream().collect(Collectors.groupingBy(DeductPersonVO::getDeductScore)).entrySet()
.stream().sorted((s1, s2) -> -Double.compare(s1.getKey(), s2.getKey())).collect(Collectors.toList());
int index = 1;
for (Map.Entry<Double, List<DeductPersonVO>> entry : list) {
map.put(entry.getKey(),index);
entry.getValue().forEach((s) -> System.out.print(" " + s.getDeductScore()));
System.out.println();
index++;
}
return map;
}
取值存入实体
Map<Double,Integer> rankMap2 = rankScore(personList);
personList.stream().forEach(bean -> {
bean.setRank(rankMap2.get(bean.getDeductScore()));
});