需求:
根据List的某个值进行从大到小的排序
代码如下:
List<Integer> ListData = new ArrayList<>();
ListData.add(1);
ListData.add(2);
ListData.add(3);
ListData.add(4);
Collections.sort(ListData,new Comparator(){
@Override
public int compare(Object o1, Object o2) {
int diff = (Integer)o1 -(Integer) o2;
if (diff>0){
return -1;
}
else if (diff <0){
return 1;
}
else {
return 0;
}
}
});
for (Integer item :ListData){
Log.i("test","item:"+item);
}
输出结果如下:成功实现了从大到小的排序