Help to understand the logic of the program. We have 3 arrays of strings, they are written in ArrayList, I sort them by the number of elements in the sheet. What I can not understand: how is this sorting done? What's going on here?

Comparator<String[]> sortByLength = new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { return o1.length - o2.length; } }; 

 public class T{ public static void main(String[] args) { String[] array1 = {"ΠΌΠ°ΠΌΠ°", "ΠΌΡ‹Π»Π°", "Ρ€Π°ΠΌΡƒ"}; String[] array2 = {"я", "ΠΎΡ‡Π΅Π½ΡŒ", "люблю", "java"}; String[] array3 = {"ΠΌΠΈΡ€", "Ρ‚Ρ€ΡƒΠ΄", "ΠΌΠ°ΠΉ"}; List<String[]> arrays = new ArrayList<>(); arrays.add(array1); arrays.add(array2); arrays.add(array3); Comparator<String[]> sortByLength = new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { return o1.length - o2.length; } }; arrays.sort(sortByLength); for (String[] str : arrays) { System.out.println(Arrays.toString(str)); } } } 

    2 answers 2

    Sorting takes place here arrays.sort(sortByLength);
    arrays is your ArrayList object. It has a sort method to which to sort you pass an object that implements the Comparator interface.

    In this code block

     Comparator<String[]> sortByLength = new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { return o1.length - o2.length; } }; 

    You implement an anonymous class using the Comparator interface. It has a compare method that accepts two objects and returns for sorting.
    0 : if objects are equal
    -1 (not necessarily -1, there must be any negative number): if o1 <o2
    1 (any positive number): if o1> o2

    UDP This code is old. up to Java 8. In Java 8, there appeared the Lamba functions that give more elegant code, like this:

     arrays.sort((o1, o2) -> o1.length - o2.length); /// ΠΈΠ»ΠΈ arrays.sort(Comparator.comparingInt(o -> o.length)); 
    • Thank you, I'm just trying to deal with lambda expressions. Could you please tell me how the arrays of strings are passed to the compare method, I mean, there are only two parameters (String [] o1, String [] o2), how are all the elements of the list being sorted out then? Or is it all a matter of implementing the sort method? - Makaroni
    • @Makaroni if ​​IDEA clicks on the sort method, then you can see its implementation. and there it is implemented. In the List interface, a transition occurs - Farkhod Daniyarov

    The comparator compares the current value and the following. That is, you will compare something like this

     {"я", "ΠΎΡ‡Π΅Π½ΡŒ", "люблю", "java"} > {"ΠΌΠ°ΠΌΠ°", "ΠΌΡ‹Π»Π°", "Ρ€Π°ΠΌΡƒ"} = 1 {"ΠΌΠΈΡ€", "Ρ‚Ρ€ΡƒΠ΄", "ΠΌΠ°ΠΉ"} < {"я", "ΠΎΡ‡Π΅Π½ΡŒ", "люблю", "java"} = -1 {"ΠΌΠΈΡ€", "Ρ‚Ρ€ΡƒΠ΄", "ΠΌΠ°ΠΉ"} < {"я", "ΠΎΡ‡Π΅Π½ΡŒ", "люблю", "java"} = 1 {"ΠΌΠ°ΠΌΠ°", "ΠΌΡ‹Π»Π°", "Ρ€Π°ΠΌΡƒ"} < {"ΠΌΠΈΡ€", "Ρ‚Ρ€ΡƒΠ΄", "ΠΌΠ°ΠΉ"} = 0 

    Based on these values, the comporator will put in ascending order depending on the length of the array.

     [ΠΌΠ°ΠΌΠ°, ΠΌΡ‹Π»Π°, Ρ€Π°ΠΌΡƒ] [ΠΌΠΈΡ€, Ρ‚Ρ€ΡƒΠ΄, ΠΌΠ°ΠΉ] [я, ΠΎΡ‡Π΅Π½ΡŒ, люблю, java] 
    • No need to lie about the "current value and the following ." You might think that the comparator goes through the array and compares 2 neighboring elements - Andrew Bystrov
    • @AndrewBystrov and how then does a walk through all the arrays take place? - Dred
    • it all depends on the sorting that applies. The comparator can easily take the first and last elements. I wrote a comment exactly to the word "next" - Andrew Bystrov