Sort an array in descending order in Java
This post will discuss how to sort an array of primitives in descending order in Java.
It’s not possible to sort an array of primitives in “descending” order using the Arrays.sort() and Collections.sort() method, since the comparator work on the Wrapper classes and objects instead of the primitives. For instance, the following code reverse sort an Integer array using the Arrays.sort() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { Integer[] arr = {4, 2, 3, 1, 5}; Arrays.sort(arr, Comparator.reverseOrder()); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
However, there are several indirect ways to sort an array of primitives in reverse order:
1. Java 8 Stream API
The idea is to use Java 8 Streams to box the primitive int to Integer and then sort it backwards using the Collections.reverseOrder() comparator. Then, collect the sorted sequence back into a primitive array. This is demonstrated below:
|
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.Collections; public class Main { public static void main(String[] args) { int[] arr = {4, 2, 3, 1, 5}; int[] sortedDesc = Arrays.stream(arr) // or use, IntStream.of(arr) .boxed() .sorted(Collections.reverseOrder()) .mapToInt(Integer::intValue) .toArray(); System.out.println(Arrays.toString(sortedDesc)); } } |
Output:
[5, 4, 3, 2, 1]
2. Using Guava
Guava’s asList(int...) method returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). We can use it to get a list view of the array and then sort it using the Collections.sort() method.
The following code demonstrates this by applying the Collections.reverseOrder() comparator to the underlying primitive array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { int[] arr = {4, 2, 3, 1, 5}; Collections.sort(Ints.asList(arr), Collections.reverseOrder()); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
Here’s an example using List.sort() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.Comparator; public class Main { public static void main(String[] args) { int[] arr = {4, 2, 3, 1, 5}; Ints.asList(arr).sort(Comparator.reverseOrder()); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 4, 3, 2, 1]
3. Sort and Reverse
Another plausible, simple, but less efficient solution, is to sort the array in ascending order and reverse it. The following code demonstrates this Stream API.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int[] arr = {4, 2, 3, 1, 5}; int[] sorted = Arrays.stream(arr).sorted().toArray(); int[] reversesorted = IntStream.rangeClosed(1, sorted.length) .map(i -> sorted[sorted.length-i]) .toArray(); System.out.println(Arrays.toString(reversesorted)); } } |
Output:
[5, 4, 3, 2, 1]
That’s all about sorting an array in descending 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 :)