创建一个实体类student
public class Student {
private Integer stuId;
private Integer scoure;
private Integer rank;
}
List<Student> list=new ArrayList<>();
list.add(new Student(123,100,null));
list.add(new Student(124,99,null));
list.add(new Student(129,56,null));
list.add(new Student(125,88,null));
list.add(new Student(126,88,null));
list.add(new Student(128,22,null));
list.add(new Student(127,88,null));
}
排序方法
public static List<Student> mergerList(List<Student> list){
//list降序排序
List<Student> collect = list.stream().sorted(Comparator.comparing(Student::getScoure)).collect(Collectors.toList());
//翻转list
Collections.reverse(collect);
Integer index=1;
Integer maxScore=0;
for (int i = 0; i < list.size(); i++) {
if (i==0){
collect.get(i).setRank(index);
maxScore=collect.get(i).getScoure();
}else if (Objects.equals(collect.get(i).getScoure(),maxScore)){
collect.get(i).setRank(index);
}else {
index++;
collect.get(i).setRank(index);
maxScore=collect.get(i).getScoure();
}
}
return collect;
}
输出结果