Java List sort() Tutorial

In this section, we will learn what the List sort() method is and how to use it in Java.

What is Java List sort() Method?

The Java List sort() method, as the name of the method suggests, is used to sort the elements of a collection object.

For example, you might have a collection with integer numbers, but these elements are set in the collection with no specific order!

Well, using this method we can sort such list to have a natural order or reverse order, etc.

This method takes a reference to a method and inside that method is the place we run the comparison operation between the elements of a collection object and decide which element should come after the other.

Basically, the return value of this reference method is of type Integer and it takes two arguments (Two elements of the target collection object at a time).

Now, if in the target reference method the first argument is bigger than the second one, the return value of the method should be +1.

If the first argument is less than the second argument, then the result of this reference method should be -1.

And if the two arguments are equally the same, then the result should be 0.

Using this pattern, the elements of the target collection will be in natural order.

But we can change the values of +1 and -1 (meaning if the first argument was bigger than the second one, then return -1 instead) and in that case the elements of the target list will be sorted in the reverse order.

Java List sort() Method Syntax:

default void sort(Comparator<? super E> c)

sort() Method Parameters:

There’s one parameter for this method and that is of a type the functional interface Comparator.

That means we need to pass a reference to a method that matches the method in this interface.

The signature of this method is:

  • The return value of this method is of type int.
  • The reference method needs to take two arguments and they are of the same type as the elements of the target collection object.

Now we can create this reference method using the lambda expression, or use a reference to an instance or static method that is already created and has the same signature.

sort() Method Return Value:

The return value of this method is void.

sort() Method Exceptions:

The method might throw three types of exceptions:

ClassCastException: This exception is thrown if the datatype of the elements in the target list is not compatible with the types of the parameters in the reference method.

UnsupportedOperationException: You get this exception if the method is used by a collection that does not support it.

IllegalArgumentException: We use this exception if the comparator is found to violate the Comparator contract.

Example: using List sort() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        
        List<String> list = new ArrayList<>();
        list.add("Omid");
        list.add("Jack");
        list.add("Ellen");
        list.add("John");

        list.sort((e1,e2)->{
            return e1.compareToIgnoreCase(e2); 
        });
        for (String s: list){
         System.out.println(s);
        }
    }
}

Output:

Ellen

Jack

John

Omid

As you can see, using the sort method, we could sort the elements of this list alphabetically.

Note: check the String compareToIgnoreCase() method if you’re not familiar with this method.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies