How to Sort ArrayList in Java?

Last Updated : 12 Jan 2026

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. Arranging data or elements in a sequence (ascending or descending) is known as sorting.

Java ArrayList

In JavaArrayList 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.

There are multiple ways to sort AarrayList but it depends on what you are going to sort (primitive wrapper, custom object, ascending/descending, custom logic).

  • Using Collections.sort() Method
  • Using Collections.reverseOrder() Method
  • Using Comparable Interface
  • Using comparator Interface

Collections.sort() Method

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.

Syntax

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 following code snippet.

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.

Collections.reverseOrder() Method

If we want to sort ArrayList in descending order, Java Collections class provides Collections.reverseOrder() method. It allows us to sort the ArrayList in reverse-lexicographic order.

Syntax

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.

Example

Compile and Run

Output:

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.

Example

Compile and Run

Output:

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.

Example

Compile and Run

Output:

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]

Let's see another example that sorts an ArrayList of Integer type.

Example

Compile and Run

Output:

ArrayList Before Sorting:
566
230
123
110
689
12
95
ArrayList After Sorting:
689
566
230
123
110
95
12

Using Comparable Interface

If you have a custom class and want to sort objects of that class in an ArrayList, you can make the class implement the Comparable interface. This interface requires implementing the compareTo() method, which defines the natural ordering of objects.

Here's an example of sorting a list of custom objects using the Comparable interface:

Example

Compile and Run

Output:

Sorted ArrayList: [Student{name='Alice', age=20}, Student{name='Bob', age=22}, Student{name='Charlie', age=21}]

Using Comparator Interface

If you want to sort objects based on criteria other than their natural ordering, you can use the Comparator interface. This interface requires implementing the compare() method, which compares two objects based on a specified criterion.

Example

Compile and Run

Output:

Sorted ArrayList: [Student{name='Alice', age=20}, Student{name='Bob', age=22}, Student{name='Charlie', age=21}]

Conclusion

Sorting an ArrayList in Java is a fundamental operation that can be achieved using various techniques. Whether we are dealing with built-in types or custom objects, Java offers flexible methods to sort collections efficiently. By understanding these techniques, we can effectively manage and manipulate data in your Java programs.


Next TopicJava ArrayList