错误写法:
List<Tt> sortedList = list.stream() .sorted(Comparator.comparing(Tt::getId)).sorted(Comparator.comparing(Tt::getLine))
.collect(Collectors.toList());
正确写法
List<Tt> sortedList2 = list2.stream().sorted(Comparator.comparing(Tt::getId).thenComparing(Tt::getLine)).collect(Collectors.toList());
更新 2021/7/30
上面的写法,没有进行空指针处理,使用Comparator.nullsLast。如下解决
List<LogicWarehouse> logicWareHouseSorts = logicWarehouses.stream().sorted(
//Comparator.nullsLast 避免空指针
Comparator.comparing(LogicWarehouse::getPriority,Comparator.nullsLast(Integer::compareTo))
.thenComparing(LogicWarehouse::getCreateTime,Comparator.nullsLast(Date::compareTo))).collect(Collectors.toList());
评论区