I want to sort the list based on the date field between the two days, say from now and the next 3 days. The sorted list should the days from now and the next 3 days in ascending order and all previous and future dates in ascending order after that.
e.g.:
List<LocalDate> list = Arrays.asList(
LocalDate.of(2021, 8, 1),
LocalDate.of(2021, 8, 2),
LocalDate.of(2021, 8, 3),
LocalDate.of(2021, 8, 8),
LocalDate.of(2021, 8, 9),
LocalDate.of(2021, 8, 10),
LocalDate.of(2021, 8, 11),
LocalDate.of(2021, 8, 12),
LocalDate.of(2021, 8, 13),
LocalDate.of(2021, 8, 14),
LocalDate.of(2021, 8, 15),
null);`
Assumed today is 8 Aug 2021 out would be:
null (null date always at the top)
8-Aug-2021
9-Aug-2021
10-Aug-2021 (next 3 days up to here in ascending order)
8-Aug-2021 ( from here all past and future dates in ascending order here)
1-Aug-2021
2-Aug-2021
3-Aug-2021
11-Aug-2021
12-Aug-2021
13-Aug-2021
14-Aug-2021
15-Aug-2021
Comparator<LocalDate> comp = (o1, o2) -> {
if (o1.isBefore(now) && o2.isBefore(now) || o1.isAfter(now.plusDays(3)) && o2.isAfter(now.plusDays(3))) {
return o1.compareTo(o2);
}
return o2.compareTo(o1);
};
8-Aug-2021, which appears only once in the example input?