List<Map>根据多个字段排序,遇value为空

重点

Comparator<Map> aa = Comparator.comparing(a -> (Integer) a.get("schoolId"));
Comparator<Map> bb = Comparator.comparing(a -> (Integer) a.get("gradeId"),Comparator.nullsLast(Integer::compareTo));
list1.sort(aa.thenComparing(bb));
public static void main(String[] args) {
    //list准备
    Map map1 = new HashMap();
    map1.put("schoolId",19);
    map1.put("gradeId",3);
    Map map2 = new HashMap();
    map2.put("schoolId",19);
    map2.put("gradeId",2);
    Map map3 = new HashMap();
    map3.put("schoolId",17);
    map3.put("gradeId",1);
    Map map4 = new HashMap();
    map4.put("schoolId",1);
    map4.put("gradeId",2);
    Map map5 = new HashMap();
    map5.put("schoolId",64);
    map5.put("gradeId",2);
    Map map6 = new HashMap();
    map6.put("schoolId",5);
    map6.put("gradeId",2);
    Map map7 = new HashMap();
    map7.put("schoolId",19);
    map7.put("gradeId",4);
    Map map8 = new HashMap();
    map8.put("schoolId",1);
    map8.put("gradeId",32);

    List<Map> list1 = new ArrayList<>();
    list1.add(map1);
    list1.add(map2);
    list1.add(map3);
    list1.add(map4);
    list1.add(map5);
    list1.add(map6);
    list1.add(map7);
    list1.add(map8);


    Comparator<Map> aa = Comparator.comparing(a -> (Integer) a.get("schoolId"));
    Comparator<Map> bb = Comparator.comparing(a -> (Integer) a.get("gradeId"),Comparator.nullsLast(Integer::compareTo));
    list1.sort(aa.thenComparing(bb));
    System.out.println(list1);


}

你可能感兴趣的:(业务使用)