How to Sort ArrayList in Java
In Java, Collection is a framework that provides interfaces (Set, List, Queue, etc.) and classes (ArrayList, LinkedList, etc.) to store the group of objects. These classes store data in an unordered manner. Sometimes we need to arrange data in an ordered manner which is known as sorting. The sorting can be performed in two ways either in ascending or descending order.
In this section, we will learn how to sort ArrayList in ascending and descending order.
In Java, ArrayList is a class of Collections framework that is defined in the java.util package. It inherits the AbstractList class. It dynamically stores the elements. The advantage of ArrayList is that it has no size limit. It is more flexible than the traditional array. It may have duplicate elements. We can also use all the methods of List interface because it implements the List interface.
We can sort an ArrayList in two ways ascending and descending order. The Collections class provides two methods to sort an ArrayList in Java.
An ArrayList can be sorted by using the sort() method of the Collections class in Java. It accepts an object of ArrayList as a parameter to be sort and returns an ArrayList sorted in the ascending order according to the natural ordering of its elements.
Remember: All elements in the ArrayList must be mutually comparable, else it throws ClassCastException. Here, mutually comparable means the list must have the same type of elements. For example, consider the snippet of the code:
In the above example, we see that a list has four elements out of which three elements are of String type and one is Integer type. The three elements that are in String are mutually comparable but the element that is of Integer type is not comparable with the other three. Hence, the list must have the same type of elements.
If we want to sort ArrayList in descending order, Java Collections class provides reverseOrder() method. It allows us to sort the ArrayList in reverse-lexicographic order.
It returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.
Remember that we do not directly invoke the reverseOrder() method. We use it along with the Collection.sort() method, as follows.
Therefore, the sorting of ArrayList in descending order done in two steps, first the ArrayList sorts the data in ascending order, after that the sorted data is reversed by the reverseOrder() method.
Let's create programs that sort ArrayList in ascending order.
Sort ArrayList in Ascending Order
In the following example, we have created an ArrayList of type String and added some elements into it. After that we have invoked sort() method of the Collections class and passed the object of the ArrayList class i.e., list that sorts the elements in the ascending order.
Before Sorting: [Volkswagen, Toyota, Porsche, Ferrari, Mercedes-Benz, Audi, Rolls-Royce, BMW] After Sorting: [Audi, BMW, Ferrari, Mercedes-Benz, Porsche, Rolls-Royce, Toyota, Volkswagen]
Let's see another example that sorts an ArrayList of Integer type.
ArrayList Before Sorting: 55 34 98 67 39 76 81 ArrayList After Sorting: 34 39 55 67 76 81 98
Sort ArrayList in Descending Order
In the following example, we have created an ArrayList of type String and added some elements into it. After that we have invoked reverseOrder() method along with the sort() method of the Collections class and passed the object of the ArrayList class i.e., list that sorts the elements in the descending order.
Before Sorting: [Data Science, Testing, C#, Basic Language, UML, Algorithms, Computer Networks, Python] After Sorting: [UML, Testing, Python, Data Science, Computer Networks, C#, Basic Language, Algorithms]
ArrayList Before Sorting: 566 230 123 110 689 12 95 ArrayList After Sorting: 689 566 230 123 110 95 12