一、失效原因
Comparator<XXX> timeDescCom = Comparator.comparing(XXX::getStartTime).reversed();
XXXList.stream().sorted(timeDescCom).collect(Collectors.toList());
//跟Collections不一样,这里要有个赋值操作。
XXXList = XXXList.stream().sorted(timeDescCom).collect(Collectors.toList());
二、创建比较器+stream的方式(有效)
Comparator<XXX> timeDescCom = Comparator.comparing(XXX::getStartTime).reversed();
XXXList = XXXList.stream().sorted(timeDescCom).collect(Collectors.toList());
之后参照链接用户对问题“Java comparator.comparing(o->x).reversed不工作”的回答 - 问答 - 腾讯云开发者社区-腾讯云可以使用下面这两种书写方式
Comparator<XXX> timeDescCom = Comparator.comparing((XXX xxx) -> xxx.getStartTime()).reversed();
Comparator<XXX> timeDescCom = Comparator.comparing((XXX xxx) -> xxx.getStartTime(),Comparator.reverseOrder());
三、创建比较器+Collections工具(有效)
Comparator<XXX> timeDescCom = Comparator.comparing(XXX::getStartTime).reversed();
Collections.sort(xxxList, timeDescCom);
//这里不需要做赋值操作
最后还有一种兜底方案是:时间差正序
Comparator.comparing(xxx -> now.time()-xxx.getStartTime())