Sorting ArrayList in Reverse or Descending Order in Java 8
A quick guide on how to sort the arraylist in descending order or reverse order in java and example programs using Collections.reverseOrder() method.
1. Overview
In this article, We will learn how to sort ArrayList in descending order in java. Sometimes this is referred as collections reverse or decreasing order.
To get the list in the reverse order, we need to use Collections.reverseOrder() and Collections.sort() methods together.
2. Sort ArrayList In Descending order using Collections.reverseOrder()
We have already shown how to sort list in ascending order using Collections.sort() method.
In the below examples, we are using the built-in comparator from the reverseOrder() method and passing it to the Collections.sort() method.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.javaprogramto.java8.arraylist; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ArrayListReverseOrder1 { public static void main(String[] args) { // Creating ArrayList List<Integer> numbersList = new ArrayList<>(); // Adding values to List numbersList.add( 150 ); numbersList.add( 50 ); numbersList.add( 250 ); numbersList.add( 500 ); numbersList.add( 350 ); // printing before sorting System.out.println( "Before sorting : " + numbersList); // Getting the descending order comparator Comparator<Integer> reverseComparator = Collections.reverseOrder(); // Sorting with the reverse comparator with sort() method. // sort() method internally uses this comparator to sort in the descending order Collections.sort(numbersList, reverseComparator); // printing the final list after reverse order sorting. Original list only // sorted. System.out.println( "After sorting : " + numbersList); } } |
Output:
1 2 | Before sorting : [ 150 , 50 , 250 , 500 , 350 ] After sorting : [ 500 , 350 , 250 , 150 , 50 ] |
3. Sort ArrayList In Descending order using Collections.reverse()
Next, look at the another way to sort the arraylist in descending order using two methods as below.
Collections.sort(arraylist); –> first sorts the list in the ascending order
Collections.reverse(arraylist); –> Next, reverse the sorted list.
Example:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListReverseOrder2 { public static void main(String[] args) { // Creating ArrayList List<Integer> numbersList = new ArrayList<>(); // Adding values to List numbersList.add( 150 ); numbersList.add( 50 ); numbersList.add( 250 ); numbersList.add( 500 ); numbersList.add( 350 ); // printing before sorting System.out.println( "Before sorting : " + numbersList); // sorting the list in the ascending order Collections.sort(numbersList); // reversing the sorted list into descending order Collections.reverse(numbersList); // printing the final list after reverse order sorting. Original list only // sorted. System.out.println( "After sorting : " + numbersList); } } |
This program also produces the same output as in the section 2.
4. Java 8 Sort ArrayList Descending Order
Sorting list in reverse order is pretty easy from stream.sorted(Collections.reverseOrder()) in java 8 api.
We can use parallelStream() method to work efficiently with larger data volumes.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class ArrayListReverseOrder2 { public static void main(String[] args) { // Creating ArrayList List<Integer> numbersList = new ArrayList<>(); // Adding values to List numbersList.add( 150 ); numbersList.add( 50 ); numbersList.add( 250 ); numbersList.add( 500 ); numbersList.add( 350 ); // printing before sorting System.out.println( "Before sorting : " + numbersList); List<Integer> descendingList = numbersList.stream() .sorted(Collections.reverseOrder()) .collect(Collectors.toList()); // printing the final list after reverse order sorting. Original list only // sorted. System.out.println( "After sorting : " + descendingList); } } |
5. Conclusion
In this article, We have seen how to sort the ArrayList in descending order in older and new java 8 streams.
How to sort Map by values in java 8?
Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Sorting ArrayList in Reverse or Descending Order in Java 8 Opinions expressed by Java Code Geeks contributors are their own. |