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);


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值