0

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]]
11
  • What do you mean? The same order: list2 = new ArrayList<>(list1)... Commented Jul 27, 2021 at 15:32
  • I'd like to have the same order of the list, if you can see the frst element of the first list is siuated in the last element of the second list Commented Jul 27, 2021 at 15:34
  • 1
    Please identify what makes an item in the list greater than or less than or equal to others. The sub-arrays (eg [2021, 06, 24]) look like they may represent dates. Is that right? Commented Jul 27, 2021 at 15:37
  • so decreasing date order ? Commented Jul 27, 2021 at 15:38
  • @StvnBrkdll Yes they rapresent date Commented Jul 27, 2021 at 15:38

3 Answers 3

3

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]]
Sign up to request clarification or add additional context in comments.

5 Comments

this return a void i'd like to return a list<List<String>>
@ValerioAuricchio then just add return values. Keep in mind that this is just an example and making use of a main(...) method which has void return type. This focuses on sorting the lists, not on returning them which is something you'd have to add if you need that.
@ValerioAuricchio what do you mean ? that is the main method os yes it is void
@azro when i use the method I'm expecting to recive a list<List<String>> in this case i reciv a void why?
@ValerioAuricchio receive from where ? How do you run the code ? Don't you know the purpose of the static void main method ?
1

[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());

5 Comments

Wen i try to do myList::toString i recive this error "Cannot resolve method 'toString'"
@ValerioAuricchio This answer says List::toString, not list::toString or myList::toString.
I wrote this : List<List<String>> sortedPartitionsValues = partitionValues.sort(List::toString, Comparator.reverseOrder()); And still recive this error "Non-static method cannot be referenced from a static contex"
@ValerioAuricchio, List::sort method is void and the sorting applied to the given instance of list, in your case it is partitionValues.
@ValerioAuricchio, you may check the update
0

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]

3 Comments

Don't suggest solutions using the old date API, especially not using Calendar. Have a look at LocalDate etc. (basically the java.time api). This is way easier to use and less error-prone.
@Thomas good suggestion, i'll revise it. However Calendar is not deprecated (yet), so...
Well, Calendar probably won't get deprecated any time soon but the same is true for AWT and other parts of the JDK and even other libraries. That doesn't mean we should suggest them for new solutions though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.