java根据list中的对象某个属性排序
Posted 电影公众号
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java根据list中的对象某个属性排序相关的知识,希望对你有一定的参考价值。
public class Test {
public static void main(String[] args) throws Exception {
CityModel city1 = new CityModel();
city1.setCity_code(1);
CityModel city2 = new CityModel();
city2.setCity_code(1);
CityModel city3 = new CityModel();
city3.setCity_code(1);
CityModel city4 = new CityModel();
city4.setCity_code(1);
CityModel city5 = new CityModel();
city5.setCity_code(1);
ArrayList<CityModel> list = new ArrayList<>();
list.add(city1);
list.add(city2);
list.add(city3);
list.add(city4);
list.add(city5);
Collections.sort(list, new Comparator<CityModel>(){
public int compare(CityModel o1, CityModel o2) {
//按照CityModel的city_code字段进行降序排列
if(o1.getCity_code() < o2.getCity_code()){
return 1;
}
if(o1.getCity_code() == o2.getCity_code()){
return 0;
}
return -1;
}
});
System.out.println(list);
}
}
以上是关于java根据list中的对象某个属性排序的主要内容,如果未能解决你的问题,请参考以下文章