举例:按学生成绩表的语文、数学加起来的总分排序,总分相同按语文排序:
Collections.sort(scoreList,(Score s1, Score s2) -> { // socreList: 待排序的list;Score:对象
if (s1.getChinese() == null) s1.setChinese(0);
if (s2.getMath() == null) s2.setMath(0);
if (s1.getChinese() == null) s1.setChinese(0);
if (s2.getMath() == null) s2.setMath(0);
int sum1 = s1.getChinese() + s1.getMath();
int sum2 = s2.getChinese() + s2.getMath();
if(sum1 > sum2) return -1;
else if (sum1 < sum2) return 1;
else{
if (s1.getChinese() >= s2.getChinese()){
return -1;
}else{
return 1;
}
}
});