多种条件排序刚开始的时候我也一脸懵逼,但是在某人的启发下,我才知道使用comparator接口,使用comparator
以下代码是摘抄别的地方:
Collections.sort(employeeList, comparator);
private Comparator<Employee> comparator = new Comparator<Employee>() {
@Override
public int compare(Employee employee1, Employee employee2) {
int cr = 0; //按级别降序排列
int a = employee2.getLevel() - employee1.getLevel();
if (a != 0) {
cr = (a > 0) ? 3 : -1;
}
else { //按薪水降序排列
a = employee2.getSalary() - employee1.getSalary();
if (a != 0) {
cr = (a > 0) ? 2 : -2;
} else
{
//按入职年数降序排列
a = employee2.getYear() - employee1.getYear();
if (a != 0) {
cr = (a > 0) ? 1 : -3;
}
}
}
return cr;
}
};
long rank=1L;
for(Employee employee :employeeList){
employee.setRank(Rank);
flag++;
}