Sort as date
As your date represents dates, the easier to compare them is to build date objets, using LocalDate.
From a List<String> o to LocalDate :
List<String> o = List.of("2021", "04", "19");
LocalDate.of(Integer.parseInt(o.get(0)), Integer.parseInt(o.get(1)), Integer.parseInt(o.get(2)))
Then apply this logic with List.sort
List<List<String>> values = Arrays.asList(List.of("2021", "04", "19"),
List.of("2021", "06", "22"), List.of("2021", "06", "24"));
values.sort((o1, o2) -> LocalDate.of(Integer.parseInt(o2.get(0)), Integer.parseInt(o2.get(1)), Integer.parseInt(o2.get(2)))
.compareTo(LocalDate.of(Integer.parseInt(o1.get(0)), Integer.parseInt(o1.get(1)), Integer.parseInt(o1.get(2)))));
System.out.println(values); // [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]
You can extract the parser into a method and use Comparator.comparing
class DateSorter {
public static void main(String[] args) {
List<List<String>> values = Arrays.asList(List.of("2021", "04", "19"),
List.of("2021", "06", "22"), List.of("2021", "06", "24"));
values.sort(Comparator.comparing(DateSorter::toDate, Comparator.reverseOrder()));
System.out.println(values); // [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]
}
static LocalDate toDate(List<String> o) {
return LocalDate.of(Integer.parseInt(o.get(0)), Integer.parseInt(o.get(1)), Integer.parseInt(o.get(2)));
}
}
Sort as string
You can also just sort each list a string, in case you have strings as doubled digits like 04 and not 4
List<List<String>> values = Arrays.asList(List.of("2021", "04", "19"),
List.of("2021", "06", "22"), List.of("2021", "06", "24"));
values.sort(Comparator.comparing(List::toString, Comparator.reverseOrder()));
System.out.println(values); // [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]
list2 = new ArrayList<>(list1)...