In this post, we will discuss several ways to sort list in reverse order in Java. The given list is modifiable, but not necessarily resizable.
1. Collections.sort()
The java.util.Collections
utility class provides 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()
:
1. Collections.sort()
+ Collections.reverse()
Here, we simply sort the list in natural order and reverse it to obtain the reverse sorted order.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.Collections; import java.util.List; // Sort List in Reverse Order in Java class SortList { 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]
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.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.Collections; import java.util.List; // Sort List in Reverse Order in Java class SortList { 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]
3. Using Custom Comparator
We can also implement our own Comparator to sort list in reverse order as shown below:
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 SortList { 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. List.sort()
Every List
implementation provides static sort()
method that sorts the list according to the order induced by the specified Comparator. For this method to work, all elements of the list must be mutually comparable using the specified comparator.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.Comparator; import java.util.List; // Sort List in Reverse Order in Java class SortList { 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. Java 8
We can sort a list in reverse order using Stream API in Java 8 if all elements of the list are mutually Comparable. Below 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()
.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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 SortList { 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]
Thanks for reading.
Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
Leave a Reply