How to sort ArrayList in java

Let’s learn how to sort ArrayList in java.

How to sort ArrayList in java

In java, ArrayList or LinkedList class objects are stored in unordered way. So to sort an ArrayList we are using Collections.sort() and reverseOrder() method.

In this post we are going to sort arraylist in java in ascending and descending order.

Collections.sort() method – Sorts the specified list into ascending order, according to the natural ordering of its elements.

Syntax:

public static <T extends Comparable<? super T>> void sort(List<T> list)

Parameters:

<T> class of the objects in the list.

list the list to be sorted.

All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

import java.util.ArrayList;
import java.util.Collections;
public class SortArrayListAscendingOrder
{
   public static void main(String[] args) 
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("Banana");
      al.add("Pineapple");
      al.add("Jackfruit");
      al.add("Apple");
      al.add("Mango");
      // unsorted given list
      System.out.println("ArrayList of Strings before sorting: ");
      for(String count : al)
      {
         System.out.println(count);
      }
      Collections.sort(al);
      // sorted list
      System.out.println("ArrayList of Strings after sorting: ");
      for(String str : al) 
      {
         System.out.println(str);
      }
   }
}


Output:

ArrayList of Strings before sorting:
Banana
Pineapple
Jackfruit
Apple
Mango
ArrayList of Strings after sorting:
Apple
Banana
Jackfruit
Mango
Pineapple


Collections.reverseOrder() Method – returns a comparator that imposes the reverse of the naturalordering on a collection of objects that implement the Comparable interface.

Syntax:

public static <T> Comparator<T> reverseOrder()

Parameters:

<T> the class of the objects compared by the comparator.

We don’t directly invoke reverseOrder() method. This method is used along with Collection.sort() method like this,

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

To sort an ArrayList in descending order, first ArrayList sorts data in ascending order then sorted data is reversed using reverseOrder() method.

import java.util.ArrayList;
import java.util.Collections;
public class SortArrayListDescendingOrder
{
   public static void main(String[] args)
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("Banana");
      al.add("Pineapple");
      al.add("Jackfruit");
      al.add("Apple");
      al.add("Mango");
      // print unsorted ArrayList   
      System.out.println("Before Sorting: " + al);   
      // sort ArrayList in ascending Order   
      // using Collection.sort() method   
      Collections.sort(al, Collections.reverseOrder());   
      // sorted ArrayList
      System.out.println("After Sorting: " + al);
   }
}


Output:

Before Sorting: [Banana, Pineapple, Jackfruit, Apple, Mango]
After Sorting: [Pineapple, Mango, Jackfruit, Banana, Apple]


Also read – operators in java