How to Sort ArrayList in Java Descending Order

By |2018-03-25T07:49:15+05:30March 25th, 2018|java|

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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment