How to Sort ArrayList in Descending order in Java

I have already shared sorting of ArrayList in ascending order. In this post, we will learn how to sort an ArrayList in descending order (decreasing order). There are 2 ways to achieve our goal:

1. Using Collections.reverseOrder()

2. Using Collections.sort()

Read Also: How to Synchronize ArrayList in Java with Example

Sorting of ArrayList in Descending Order

1. Using Collections.reverseOrder() method


To sort the ArrayList in descending order we will use two methods Collections.reverseOrder() method and Collections.sort() method.

One liner will be like :

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


Code for Sorting ArrayList of String in Descending Order


import java.util.*;

 public class ArrayListDescendingSort {
    public static void main(String args[]) {
        
        ArrayList<String> arrList = new ArrayList();
        arrList.add("Apple");
    arrList.add("Banana");
    arrList.add("Pear");
    arrList.add("Mango");
    /*Unsorted List: ArrayList content before sorting*/     System.out.println("ArrayList Before Sorting:");     for(String s: arrList){
        System.out.println(s);       }     /* Sorting in decreasing (descending) order*/     Collections.sort(arrList, Collections.reverseOrder());
    /* Sorted List in reverse order*/     System.out.println("ArrayList in descending order:");     for(String str: arrList){
        System.out.println(str);     } } }


Output

ArrayList Before Sorting:
Apple
Banana
Pear
Mango
ArrayList in descending order:
Pear
Mango
Banana
Apple


Code for Sorting ArrayList of Integers in Descending Order

Above example is to sort String ArrayList. Similarly, we can sort ArrayList of Integers in descending order.


import java.util.*;

 public class ArrayListDescendingSort2 {
    public static void main(String args[]) {
        
        ArrayList<Integer> arraylist = new ArrayList();
        arraylist.add(1);
        arraylist.add(13);
        arraylist.add(89);
        arraylist.add(45);

        /*Unsorted List: ArrayList content before sorting*/
        System.out.println("ArrayList Before Sorting:");
        for(int num : arraylist){
           System.out.println(num);
        }

        /* Sorting in decreasing (descending) order*/
        Collections.sort(arraylist, Collections.reverseOrder());

        /* Sorted List in reverse order*/
        System.out.println("ArrayList in descending order:");
        for(int num: arraylist){
           System.out.println(num);
        }
    }
}


Output

ArrayList Before Sorting:
1
13
89
45
ArrayList in descending order:
89
45
13
1

2. Using Collections.sort() method


Another way of sorting ArrayList in Descending order is to sort the list in ascending order first and then it will be reversed.

Collections.sort(list);
Collections.reverse(list);


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

 public class ArrayListDescendingSort3 {
    public static void main(String args[]) {
        
        ArrayList<String> al = new ArrayList();
        al.add("Dell");
        al.add("HP");
        al.add("Apple");
        al.add("Lenovo");

        /*Unsorted List: ArrayList content before sorting*/
        System.out.println("ArrayList content before sorting: ");
        for(String s : al){
           System.out.println(s);
        }

        /* Sorting in decreasing (descending) order*/
        Collections.sort(al);
        Collections.reverse(al);

        /* Sorted List in reverse order*/
        System.out.println("ArrayList in descending order:");
        for(String str: al){
           System.out.println(str);
        }
    }
}


Output

ArrayList content before sorting:
Dell
HP
Apple
Lenovo
ArrayList in descending order:
Lenovo
HP
Dell
Apple

That's all for today. Please mention in the comments if you have any questions related to how to sort ArrayList in descending order in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry