问题遇到的现象和发生背景
本来有个项目中需要对ProvinceCasesDaily的list集合排序,如下:
@Test
void contextLoads() {
final ArrayList<ProvinceCasesDaily> list = new ArrayList<>();
final ProvinceCasesDaily provinceCasesDaily = new ProvinceCasesDaily();
list.add(new ProvinceCasesDaily().setTime(LocalDate.of(2022,6,5)));
list.add(new ProvinceCasesDaily().setTime(LocalDate.of(2022,6,3)));
list.add(new ProvinceCasesDaily().setTime(LocalDate.of(2022,6,2)));
list.add(new ProvinceCasesDaily().setTime(LocalDate.of(2022,6,22)));
list.add(new ProvinceCasesDaily().setTime(LocalDate.of(2022,6,13)));
list.add(new ProvinceCasesDaily().setTime(LocalDate.of(2022,6,27)));
final List<ProvinceCasesDaily> collect = list.stream().sorted((a,b)->{
return a.getTime().isBefore(b.getTime())?1:0;
}).collect(Collectors.toList());
System.out.println("list.stream().sorted() = " + collect);
}
但是结果并不尽人意;结果还是按照原来的顺序显示,我猜想应该是因为ProvinceCasesDaily中LocalDateTime()还是个对象的缘故,但是我想知道,如果想要正确比较的话如何写代码呢?
麻烦各位出出主意!