Pages

Sorting ArrayList in Java is a common task for Java developer and we have touched it while discussing in my last article 10 Examples of ArrayList in Java and again when we discussed comparator and comparable in Java. In order to sortan ArrayList, we need to use Collections utility class which contains overloaded sort() method for sorting different collections and supports different comparator in Java. In this article, we will see how to sort ArrayList in the natural order of element and then sorting ArrayList in Java with a comparator. The java.util.Collections class provide a reverse Comparator which can be used to sort the array in decreasing order. You can obtain this Comparator by calling the Collections.reverseOrder() method.

Sort ArrayList in Ascending and Descending order

Sorting an ArrayList has become even easier with a lambda expression, method reference, and various utility methods added on Comparable and Comparator class. See this tutorial gets a feel of sorting ArrayList in Java 8.

Sorting ArrayList in Java with natural Order

In Order to Sort Java ArrayList on the natural order of elements, the object stored in ArrayList must implement Comparable interface in Java and should override compareTo() method as per their natural order. In our example of natural order sorting in ArrayList, we have implemented compareTo of smartphone and sortedthem based on brands. So an Apple smartphone comes before Nokia smartphone. Once your object is OK just store them in Java ArrayList and pass that list to Collections.sort() method, this will sort the list in natural order of objects. See the bottom of this Java tutorialfor a complete code exampleof Sorting Java ArrayList in Natural Order. If you love books, you can also check out the Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about sorting ArrayList. One of the best book to learn core Java.

Sorting Java ArrayList with custom Order

To sort an ArrayList in Java on Custom order we need to supply an external Comparator along-with ArrayList to Collections.sort(List, Comparator) method. Compare() method will define how sorting of objects will take place in ArrayList.In our example of custom order sorting of Java ArrayList, we have created a PriceComparator which sorts objects based on their price. So you can get cheapest or expensive smartphone stored in ArrayList. See below for full code example of ArrayList Sorting in custom order in Java:

Java Program to sort an ArrayList

Here is complete code example of sorting an ArrayList in java on both natural and custom order by using Custom comparator.

How to sort ArrayList in Descending Order in Java

ArrayList can also be sorted in descending or reverse order by using Collections.reverseOrder() and Collection.reverseOrder(Comparator cmp). Former method will sort in reverse order of natural ordering while later method will sort in the reverse order of specified comparator as shown in following example of sorting arraylist into reverse order :

//sorting ArrayList in descending or reverse order in Java

List unsortedList = Arrays.asList("abc", "bcd", "ade", "cde");

Collections.sort(unsortedList, Collections.reverseOrder());

System.out.println("Arraylist in descending order: " + unsortedList);

Output:

ArrayList before sorting in reverse order: [abc, bcd, ade, cde]

Arraylist in descending order: [cde, bcd, ade, abc]

How to sort ArrayList of String in Case insensitive Order

ArrayList of String can also be sorted with case insensitive comparison. String class defines a convenient case insensitive comparator which can be accessed directly like String.CASE_INSENSITIVE_ORDER . if you pass this comparator to ArrayList.sort() which contains String then those will be sorted accordingly. Here is an example of sorting arraylist in case insensitive order:

That’s all on Sorting ArrayList in Java based on the natural order of Object and any custom order by using Custom Comparator. Let me know if you face any issue while running Sorting ArrayList Example code in Java and me wold be glad to help you.

15 comments
:

Anonymous
said...

Should have mention that Object should be implemented Comparable interface in order to them to be sorted in Arraylist in natural order. first natural order is applied and than ascending or descending which determine direction.

I really like different kind of comparator Java provides like reverse comparator which can be used to sort List in descending order, Case Insensitive Comparator which can be used to Sort array in case insensitive order. Can you please give example of sorting arraylist in two different columns e.g. first name and than surname ?

how to sort arraylist in java - Use Collections.sorthow to sort arraylist of objects in java - Use collections.sort with compareTohow to sort arraylist in java using comparator - pass Comparator to sort()how to sort arraylist in descending order in java - use reverseComparator() and pass that to sort

wow I didn't know that there is a built in Comparator exists to compare Strings in CASE INSENSITIVE order. that is kind of nice little find for me. on related note what is difference between an ordered collection or Sorted collection, isn't it both the same ? is ArrayList a sorted collection or ordered collection ?

@Anonymous, you are write compare() expect Object so its overloaded, should have used @Override to prevent that, that is very hard to find bug if you are not using @Override annotation. thanks for pointing it. I will correct it.

My compiler complained that the interfaces were not implemented. When I let it suggest corrections, it added compare(Object) and compareTo(Object,Object) methods. Further research revealed that both interfaces should have been 'parametized'. Basically, that means just adding to the Class definitions so that the first reads "private static class SmartPhone implements Comparable" and the second reads "private static class PriceComparator implements Comparator"

Apparently the interfaces are written for generic object types, so you have to specify which types you want to handle.