In this tutorials, we will see how to sort ArrayList in descending order.

Sort ArrayList:

In previous example, we have discssed about how to sort an ArrayList in ascending order, as we seen it is an default sorting order of Collections.sort() method.

We can also sort the ArrayList in reverseOrder like descending order.

Sort ArrayList in Descending Order :

To do this we need to do 2 actions on our ArrayList i.e

  1. Reverse the ArrayList using Collections.reverseOrder() method
  2. And apply Collections.sort(List<>) on the List.

Here is the Example to Sort the ArrayList in reverse order.

Create Array List :

ArrayList<String> fruitsList = new ArrayList();
fruitsList.add("Banana");
fruitsList.add("Apple");
fruitsList.add("Grapes");
fruitsList.add("Mango");

Example :

System.out.println("Before Sorting");
System.out.println("--------------");
fruitsList.forEach((fruit)->System.out.println(fruit));
System.out.println("After Sorting");
System.out.println("--------------");
Collections.sort(fruitsList,Collections.reverseOrder());
fruitsList.forEach((fruit)->System.out.println(fruit));

Output :

Before Sorting
--------------
Banana
Apple
Grapes
Mango
After Sorting
--------------
Mango
Grapes
Banana
Apple

Collections.reverse() :

The same we can do with Collections.reverse() method like below.

Collections.sort(fruitsList);
Collections.reverse(fruitsList);
fruitsList.forEach((fruit)->System.out.println(fruit));

Output :

Before Sorting
--------------
Banana
Apple
Grapes
Mango
After Sorting
--------------
Mango
Grapes
Banana
Apple

Happy Learning 🙂