简体   繁体   中英

Sorting array of array Integer where the elements to sort are int in Java

I have these two arrays of arrays of integer:

  1. [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]
  2. [[2021, 04, 19], [2021, 06, 22], [2021, 06, 24]]

I want to have the same order of the two arrays, how I can do that in Java? So I want both arrays in this order:

  1. [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]
  2. [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]

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]]

[It is possible to sort the input list using Comparator.comparing and Comparator.reverseOrder() like this:

List<List<String>> list = Arrays.asList(
    Arrays.asList("2021", "04", "19"),
    Arrays.asList("2021", "06", "22"),
    Arrays.asList("2021", "06", "24")
    );
list.sort(Comparator.comparing(List::toString, Comparator.reverseOrder()));

System.out.println("Reversed = " + list);

Output:

Reversed = [[2021, 06, 24], [2021, 06, 22], [2021, 04, 19]]

Another approach is to apply method Comparator::reversed chained immediately after Comparator.comparing like this:

list.sort(Comparator.comparing(List<String>::toString).reversed());

Update

If a new sorted list should be retrieved from some existing list, this can be implemented using Stream API: Stream::sorted and Stream::collect with Collectors.toList() collector:

List<List<String>> sortedPartitionsValues = partitionValues
        .stream()
        .sorted(Comparator.comparing(List<String>::toString).reversed())
        .collect(Collectors.toList());

This appears to solve the problem:

public class Tester {

    public static void main(String[] args) {
        int [][] ar = new int[][] {{2021, 04, 19}, {2021, 06, 22}, {2021, 06, 24}};
        List<int[]> list = Arrays.asList(ar);

        System.out.println("original:");
        list.forEach(subAr -> {
            System.out.println("[" + subAr[0] + ", " + subAr[1] + ", " + subAr[2] + "]");
        });

        list.sort((lhs, rhs) -> {
            LocalDate dlhs = LocalDate.of(lhs[0], lhs[1], lhs[2]);
            LocalDate drhs = LocalDate.of(rhs[0], rhs[1], rhs[2]);
            return drhs.compareTo(dlhs);
        });
        System.out.println("sorted:");
        list.forEach(subAr -> {
            System.out.println("[" + subAr[0] + ", " + subAr[1] + ", " + subAr[2] + "]");
        });
    }
}

Output is:

original:
[2021, 4, 19]
[2021, 6, 22]
[2021, 6, 24]
sorted:
[2021, 6, 24]
[2021, 6, 22]
[2021, 4, 19]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM