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);
}
}

文末小福利免费视频资源网站(搜索猴)www.sousuohou.com
电影公众号https://m.veikee.cn/
 
posted @ 2017-11-03 20:53  电影公众号  阅读(7670)  评论(0编辑  收藏  举报