collections 比较器(使用匿名类)
有实体类Ability,其中有List<Ability> abilities 对象集合,根据abilities中的某个字段进行排序
Collections.sort(abilities, new Comparator<Ability>() {
//解法一
//匿名类
@Override
public int compare(Ability o1, Ability o2) {
// 按照sort进行升序排列
if (o1.getSort() > o2.getSort()) {
return 1;
}
if (o1.getSort() == o2.getSort()) {
return 0;
}
return -1;
}
//解法二
//匿名类
//@Override
//public int compare(Ability o1,Ability o2){
// double i=o1.getSort()- o2.getSort();
// return (int)i;
//}
});
map.put("ranking",abilities);
System.out.println("升序排序后:" + abilities);