Sort List in reverse order in Java
This post will discuss several ways to sort a list in reverse order in Java. The given list is modifiable but not necessarily resizable.
1. Using Collections.sort()
method
The java.util.Collections
utility class provides a static sort()
method that sorts the specified list according to the order induced by the specified comparator. It sorts the specified list according to the natural ordering of its elements if no comparator is specified.
There are several methods to reverse a list using Collections.sort()
:
⮚ Collections.sort()
with Collections.reverse()
Here, we simply sort the list in the natural order and reverse it to obtain the reverse sorted order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.Collections; import java.util.List; // Sort list in reverse order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); Collections.sort(list); Collections.reverse(list); System.out.println(list); } } |
Output:
[10, 8, 6, 5, 4, 2]
⮚ Using Reverse Order Comparator
For sorting the list in reverse order, we can pass a comparator to Collections.sort()
that imposes the reverse of the natural ordering.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Collections; import java.util.List; // Sort list in reverse order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); Collections.sort(list, Collections.reverseOrder()); System.out.println(list); } } |
Output:
[10, 8, 6, 5, 4, 2]
⮚ Using Custom Comparator
We can also implement our own Comparator to sort the list in reverse order, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; class CustomComparator<T extends Comparable<T>> implements Comparator<T> { @Override public int compare(T a, T b) { return b.compareTo(a); } } // Sort list in reverse order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); Collections.sort(list, new CustomComparator()); System.out.println(list); } } |
Output:
[10, 8, 6, 5, 4, 2]
2. Using List.sort()
method
Every List
implementation provides a static sort()
method that sorts the list according to the order induced by the specified Comparator. For this method to work, all the list elements must be mutually comparable using the specified comparator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.Comparator; import java.util.List; // Sort list in reverse order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); list.sort(Comparator.reverseOrder()); System.out.println(list); } } |
Output:
[10, 8, 6, 5, 4, 2]
3. Using Java 8
We can sort a list in reverse order using Stream in Java 8 and above if all the list elements are mutually Comparable. Following are the steps:
- Obtain a stream consisting of all elements of the list.
- Sort the stream in reverse order using
Stream.sorted()
method by passingComparator.reverseOrder()
to it that imposes the reverse of the natural ordering. We can also pass a lambda expression(a, b) -> b.compareTo(a)
toStream.sorted()
method that defines the reverse sorting order. - Collect all elements of sorted stream in a list using
Stream.collect()
withCollectors.toList()
.
The following program demonstrates it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; // Sort list in reverse order in Java class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(10, 4, 2, 6, 5, 8); list = list.stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); System.out.println(list); } } |
Output:
[10, 8, 6, 5, 4, 2]
That’s all about sorting a list in reverse order in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)