Wednesday, November 12, 2025
HomeLanguagesJavaHow to sort an ArrayList in Descending Order in Java

How to sort an ArrayList in Descending Order in Java

Given an unsorted ArrayList, the task is to sort this ArrayList in descending order in Java.

Examples:

Input: Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]
Output: Sorted ArrayList: [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal]

Input: Unsorted ArrayList: [Geeks, For, ForGeeks]
Output: Sorted ArrayList: [Geeks, ForGeeks, For]

Approach: An ArrayList can be Sorted by using the sort() method of the Collections Class in Java. This sort() method takes the collection to be sorted and Collections.reverseOrder() as the parameter and returns a Collection sorted in the Descending Order. Collections.reverseOrder() acts as the comparator in this method.

Syntax:

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

Below is the implementation of the above approach:




// Java program to demonstrate
// How to sort ArrayList in descending order
  
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
  
        // Get the ArrayList
        ArrayList<String>
            list = new ArrayList<String>();
  
        // Populate the ArrayList
        list.add("Geeks");
        list.add("For");
        list.add("ForGeeks");
        list.add("GeeksForGeeks");
        list.add("A computer portal");
  
        // Print the unsorted ArrayList
        System.out.println("Unsorted ArrayList: "
                           + list);
  
        // Sorting ArrayList in descending Order
        // using Collection.sort() method
        // by passing Collections.reverseOrder() as comparator
        Collections.sort(list, Collections.reverseOrder());
  
        // Print the sorted ArrayList
        System.out.println("Sorted ArrayList "
                           + "in Descending order : "
                           + list);
    }
}


Output:

Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal]
Sorted ArrayList in Descending order : [GeeksForGeeks, Geeks, ForGeeks, For, A computer portal]
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6763 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11983 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6834 POSTS0 COMMENTS
Umr Jansen
6838 POSTS0 COMMENTS