码迷,mamicode.com
首页 > 编程语言 > 详细

java根据list中的对象某个属性排序

时间:2017-11-04 00:09:30      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:ret   list   test   print   lis   cep   属性   public   pre   

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中的对象某个属性排序

标签:ret   list   test   print   lis   cep   属性   public   pre   

原文地址:http://www.cnblogs.com/vicF/p/7780440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!