本文主要是介绍list.sort实现根据对象的属性值对集合进行排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
list.sort实现根据对象的属性值对集合进行排序,如下所示List<Map<String,Object>> list = new ArrayList<>(); Map<String,Object> map1 = new HashMap<>(); map1.put("gz_id",1); map1.put("aaa","aaa"); Map<String,Object> map2 = new HashMap<>(); map2.put("gz_id",2); map2.put("aaa","bbb"); Map<String,Object> map3 = new HashMap<>(); map3.put("gz_id",3); map3.put("aaa","ccc"); list.add(map3); list.add(map2); list.add(map1); Map<String,Object> map4 = new HashMap<>(); map4.put("gz_id",4); map4.put("aaa","aaa1"); list.add(map4); System.out.println(list.get(0).get("gz_id")); System.out.println(list.get(1).get("gz_id")); System.out.println(list.get(2).get("gz_id")); System.out.println(list.get(3).get("gz_id")); list.sort((o1, o2) -> {int type1 = (int) o1.get("gz_id");int type2 = (int) o2.get("gz_id");return type1 - type2; }); System.out.println("---------"); System.out.println(list.get(0).get("gz_id")); System.out.println(list.get(1).get("gz_id")); System.out.println(list.get(2).get("gz_id")); System.out.println(list.get(3).get("gz_id"));
这篇关于list.sort实现根据对象的属性值对集合进行排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!