List<Map<String,Object>>升降序排序

时间:2025-03-27 13:30:43
List<List<Map<String,Object>>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("name", "li"); map1.put("age", "5"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("name", "zhang"); map2.put("age", "14"); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("name", "xie"); map3.put("age", "11"); list.add(map1); list.add(map3); list.add(map2); Collections.sort(list, Comparator.comparing(o -> (((String) o.get("age"))))); //正序 System.out.println("-------------------"); for (Map<String,Object> m:resultList){ System.out.println( "Map[name=" +m.get( "name" )+ "age=" +m.get( "age" )+ "]" ); } }