How to sort ArrayList in java

Let’s learn how to sort ArrayList in java.

How to sort ArrayList in java

In java, ArrayList does not follow sorting order. So to sort an ArrayList we are using Collections class sort() method which sorts in ascending order and sorting arraylist in descending order using Comparator interface.

Collections.sort() method – Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface.

Syntax:

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

Type parameters:

<T> class of the objects in the list.

Parameters:

list the list to be sorted.

Let’s see an example.

import java.util.ArrayList;
import java.util.Collections;

public class SortArrayList
{
   public static void main(String[] args)
   {
      ArrayList al = new ArrayList();
      al.add("Banana");
      al.add("Pineapple");
      al.add("Jackfruit");
      al.add("Apple");
      al.add("Mango");
      System.out.println("Before sorting : " + al);
      Collections.sort(al);
      System.out.println("After sorting : " + al);
   }
}

Output:

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


In the above java program adding heterogeneous elements is not a problem. But calling Collections.sort() is the problem. It throws ClassCastException.

import java.util.ArrayList;
import java.util.Collections;

public class SortArrayList
{
   public static void main(String[] args)
   {
      ArrayList al = new ArrayList();
      al.add("Banana");
      al.add("Pineapple");
      al.add("Jackfruit");
      al.add("Apple");
      al.add("Mango");
      al.add(new Integer(15));
      System.out.println("Before sorting : " + al);
      Collections.sort(al);
      System.out.println("After sorting : " + al);
   }
}

Output:

Before sorting : [Banana, Pineapple, Jackfruit, Apple, Mango, 15]
Exception in thread “main” java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer


Similarly if “null” is added, it throws NullPointerException.

import java.util.ArrayList;
import java.util.Collections;

public class SortArrayList
{
   public static void main(String[] args)
   {
      ArrayList al = new ArrayList();
      al.add("Banana");
      al.add("Pineapple");
      al.add("Jackfruit");
      al.add("Apple");
      al.add("Mango");
      al.add(null);
      System.out.println("Before sorting : " + al);
      Collections.sort(al);
      System.out.println("After sorting : " + al);
   }
}

Output:

Before sorting : [Banana, Pineapple, Jackfruit, Apple, Mango, null]
Exception in thread “main” java.lang.NullPointerException


Sorting arraylist in java in descending order using Comparator interface – Sorts the specified list according to the order induced by the specified comparator.

All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastExceptionfor any elements e1 and e2 in the list).

Syntax:

Collections.sort(List<T> list, Comparator<? super T> c)

Type Parameters:

<T> the class of the objects in the list.

Parameters:

list the list to be sorted.
c the comparator to determine the order of the list. A “null” value indicates that the elements’ natural ordering should be used.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

class SortArrayListDescendingOrder
{
   public static void main(String[] args)
   {
      ArrayList al = new ArrayList();
      al.add("Banana");
      al.add("Pineapple");
      al.add("Jackfruit");
      al.add("Apple");
      al.add("Mango");
      System.out.println("Before: " + al);     
      Collections.sort(al, new MyComparator());
      System.out.println("After: " + al);
   }
}
class MyComparator implements Comparator
{
   public int compare(Object obj1, Object obj2)
   {
      String str1 = (String) obj1;
      String str2 = obj2.toString();
      return str2.compareTo(str1);
   }
}

Output:

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


Also read – operators in java