Java Collections sort() With Examples

Today we will look into Java Collections sort method. While working with Collections in java, more than often we need to sort the data.

Java Collections sort()

Java Collections class provides us with a very convenient method Collections.sort() to sort all List implementations such as LinkedList and ArrayList.

There are two overloaded Collections.sort() methods, which are:

  1. sort(List list): Sorts the elements of the List in ascending order of their natural ordering.
  2. sort(List list, Comparator c): Sorts the elements of the list according to the order induced by the comparator.

Note that the above methods signature use generics but I have removed them here for simplicity in reading. Let us one by one dig into how and when we can use both these methods.

Java Collections sort(List list)

Consider an ArrayList of String:

Now, we will sort it using Collections.sort():

The output of this program will be:

Hence, we can see that Collections.sort() has sorted the list of String in Lexical order. And it does not return anything.

What if we have a list of custom objects? Of course, we can sort them as well.
Consider a class Fruit:

Let’s create a list of Fruits:

In order to sort this list, if we directly use the Collections.sort(List list), it will give a Compile Time Error because there is no natural ordering defined for the Fruit objects. So, it doesn’t know how to sort this list.

java-collection-sort-error

For objects to have a natural order they must implement the interface java.lang.Comparable.

The Comparable interface has a method compareTo(), which returns a negative, 0, a positive if the current value is less than, equal to, or greater than the value we are comparing with, respectively.

Let’s enhance the Fruit class to implement Comparable interface. We are defining that the natural order of sorting is based on the “id” field of Fruit:

Now that we have implemented Comparable, we can sort the list without any errors:

The output will be as below:

Java Collections sort(List list, Comparator c)

In order to define a custom logic for sorting, which is different from the natural ordering of the elements, we can implement the java.util.Comparator interface and pass an instance of it as the second argument of sort().

Let’s consider that we want to define the ordering based on the “name” field of the Fruit. We implement the Comparator, and in its compare() method, we need to write the logic for comparison:

Now, we can sort it using this comparator:

The output will be as below:

Instead of writing new class for Comparator, using lambda function, we can provide sorting logic at runtime as well:

Java Collections.reverseOrder

By default, Collection.sort performs the sorting in ascending order. If we want to sort the elements in reverse order we could use following methods:

  1. reverseOrder(): Returns a Comparator that imposes the reverse of natural ordering of elements of the collection.
  2. reverseOrder(Comparator cmp): Returns a Comparator that imposes reverse ordering of the specified comparator.

Here are the examples for both these methods:

Java Collections reverseOrder() example

It’ll output the fruits in reverse alphabetical order:

Java Collections reverseOrder(Comparator cmp) example

Output:

That’s all for Java Collections sort() method and it’s examples.

Reference: API Doc

By admin

Leave a Reply