ArrayList sort Method Tutorial

void sort(Comparator<? super E> c)

The sort method sorts the current ArrayList object based on the Comparator argument. The most widely used method for sorting an ArrayList is actually the sort method from the Collections class. The method signature for the sort method is not the same as the sort method in the ArrayList class. To use the Collections sort method, you will simply pass the ArrayList reference variable as the argument and the sort method will by default sort your ArrayList object in ascending order. There is another overloaded version of the sort method in the Collections class that takes a second parameter that is a Comparator, I will demonstrate how to use that flavor as well.

The Comparator interface is a functional interface that has a single abstract method named compare that returns a primitive int result.
public interface Comparator<T> {
      int compare(T o1, T o2);
}
So basically we can pass a Comparator based lambda expression as the argument for this method to do its thing. I'll demonstrate exactly how the Comparator interface works in the source code.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListSort
C:\Java>cd ArrayListSort
C:\Java\ArrayListSort>Notepad ArrayListSort.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;
class ArrayListSort {
    public static void main(String args[]) {
        ArrayList<String> strArray = new ArrayList<>();        
        Collections.addAll(strArray, "Yellow", "Blue", "Green", "Orange", "Violet", "Red", "Pink");
        System.out.println(strArray);
        Collections.sort(strArray);
        System.out.println(strArray);
        System.out.println("\n---------\n");
        Collections.sort(strArray, Collections.reverseOrder());
        System.out.println(strArray); 
        Collections.sort(strArray);
        System.out.println(strArray);           
        strArray.sort(Collections.reverseOrder());
        System.out.println(strArray);

        ArrayList<Integer> intArray = new ArrayList<>();
        intArray.addAll(Arrays.asList(17, 3, 7, 0, 1024, 518));
        System.out.println(intArray);
        Collections.sort(intArray);
        System.out.println(intArray);
        System.out.println("\n---------\n");

        String blue = "Blue";
        String yellow = "Yellow";
        String green = "Green";
        
        
        System.out.println("green.compareTo(yellow) = " + green.compareTo(yellow));
        System.out.println("green.compareTo(green) = " + green.compareTo(green));
        System.out.println("green.compareTo(blue) = " + green.compareTo(blue));

        FunctionalInterface<String> fi;
        fi = (x,y) -> x.compareTo(y);
        System.out.println("fi.compare(green, yellow) = " + fi.compare(green, yellow));
        System.out.println("fi.compare(green, green) = " + fi.compare(green, green));
        System.out.println("fi.compare(green, blue) = " + fi.compare(green, blue));
        System.out.println("\n---------\n");

        Comparator<String> c;
        c = (x,y) -> x.compareTo(y);
        System.out.println("c.compare(green, yellow) = " + c.compare(green, yellow));
        System.out.println("c.compare(green, green)  = " + c.compare(green, green));
        System.out.println("c.compare(green, blue) = " + c.compare(green, blue));
        System.out.println("\n---------\n");
     
        strArray.sort(c);
        System.out.println(strArray); 
        c = (x,y) -> -x.compareTo(y);  
        strArray.sort(c);
        System.out.println(strArray);         
    }
}

interface FunctionalInterface<T>{
    int compare(T firstArg, T secondArg);    
}

Now switch back to the command prompt (CMD) and type in javac ArrayListSort.java and press Enter.
Now type in java ArrayListSort and press Enter.


C:\Java\ArrayListSort>javac ArrayListSort.java
C:\Java\ArrayListSort>java ArrayListSort
[Yellow, Blue, Green, Orange, Violet, Red, Pink]
[Blue, Green, Orange, Pink, Red, Violet, Yellow]

----------

[Yellow, Violet, Red, Pink, Orange, Green, Blue]
[Blue, Green, Orange, Pink, Red, Violet, Yellow]
[Yellow, Violet, Red, Pink, Orange, Green, Blue]
[71, 3, 7, 0 , 1024, 518]
[0, 3, 7, 17, 518, 1024]

------------

green.compareTo(yellow) = -18
green.compareTo(green) = 0
green.compareTo(blue) = 5
fi.compare(green, yellow) = -18
fi.compare(green, green) = 0
fi.compare(green, blue) = 5

------------

c.compare(green, yellow) = -18
c.compare(green, green) = 0
c.compare(green, blue) = 5

----------

[Blue, Green, Orange, Pink, Red, Violet, Yellow]
[Yellow, Violet, Red, Pink, Orange, Green, Blue]


Final thoughts

Understanding the Comparator compare abstract method is the key to understanding how the sort method functions.


Tutorials