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); }}
时间: 2024-10-21 05:21:41