列表中的日期格式为mm/dd/yyyy
当我使用stream以逆序对它们进行排序时,
sortedList = csvList.stream().sorted(Comparator.reverseOrder())
.collect(Collectors.toList())日期按相反顺序排序,但日期先排序2019,然后排序2020,如下所示
12/16/2019, 12/11/2019, 11/11/2019, 10/28/2019, 07/17/2020/, 07/15,2020, 06/27/2020/, 06/18/2020我希望日期排序如下
07/17/2020/, 07/15,2020, 06/27/2020/, 06/18/2020,12/16/2019, 12/11/2019, 11/11/2019, 10/28/2019有人能帮我解决这个问题吗?
发布于 2020-07-18 21:21:15
下面的lamda函数将按排序顺序给出结果。
List<String> csvList = List.of("02/12/2002", "12/16/2019", "12/11/2019", "11/11/2019", "10/28/2019", "07/17/2020", "07/15/2020", "06/27/2020", "06/18/2020");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
csvList.stream().sorted((d1, d2) -> LocalDate.parse(d1, formatter)
.compareTo(LocalDate.parse(d2, formatter))).collect(Collectors.toList());PS:您的日期格式不正确,它可能会throw parsing exception。
https://stackoverflow.com/questions/62968692
复制相似问题