students) {
// students.sort((h1, h2) -> {
// // 排名相同,年龄正序排序
// if (Double.compare(h1.getScore(), h2.getScore()) == 0) {
// return Double.compare(h1.getAge(), h2.getAge());
// }
// return -Double.compare(h1.getScore(), h2.getScore());
// });
students.sort(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getAge));
// students.forEach(System.out::println);
int index = 0;
int count = 0;
double lastScore = -1;
MaprankMap = new HashMap<>(5);
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.toString() + ",lastScore:" + lastScore + ",count:" + count + ",index:" + index);
// 如果成绩和上一名的成绩不相同,那么排名+1
if (Double.compare(lastScore, s.getScore()) != 0) {
lastScore = s.getScore();
index = index + 1 + count;
count = 0;
} else {
// 分数相同,如果年龄不同,排名+1
if (Double.compare(students.get(i - 1).getAge(), s.getAge()) != 0) {
index = index + 1 + count;
count = 0;
} else {
// 重复数+1
count++;
}
}
s.setIndex(index);
System.out.println(s.toString() + ",lastScore:" + lastScore + ",count:" + count + ",index:" + index);
System.out.println("****************");
rankMap.put(i, s);
}
for (Integer key : rankMap.keySet()) {
System.out.println(rankMap.get(key));
}
}
}