To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array.
The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays. Convert your primitives to their respective objects. Integer for int, Double for double, Boolean for boolean, etc.
Use the Reverse() method that would eventually give you a sorted array in descending order. Array. Reverse(list); You can try to run the following code to to sort an array in descending order.
We can sort arrays in ascending order using the sort() method which can be accessed from the Arrays class. The sort() method takes in the array to be sorted as a parameter. To sort an array in descending order, we used the reverseOrder() method provided by the Collections class.
You could use this to sort all kind of Objects
sort(T[] a, Comparator<? super T> c)
Arrays.sort(a, Collections.reverseOrder());
Arrays.sort()
cannot be used directly to sort primitive arrays in descending order. If you try to call the Arrays.sort()
method by passing reverse Comparator defined by Collections.reverseOrder()
, it will throw the error
no suitable method found for sort(int[],comparator)
That will work fine with 'Array of Objects' such as Integer array but will not work with a primitive array such as int array.
The only way to sort a primitive array in descending order is, first sort the array in ascending order and then reverse the array in place. This is also true for two-dimensional primitive arrays.
for a list
Collections.sort(list, Collections.reverseOrder());
for an array
Arrays.sort(array, Collections.reverseOrder());
You can use this:
Arrays.sort(data, Collections.reverseOrder());
Collections.reverseOrder()
returns a Comparator
using the inverse natural order. You can get an inverted version of your own comparator using Collections.reverseOrder(myComparator)
.
an alternative could be (for numbers!!!)
Literally spoken:
array = -Arrays.sort(-array)
without explicit comparator:
Collections.sort(list, Collections.reverseOrder());
with explicit comparator:
Collections.sort(list, Collections.reverseOrder(new Comparator()));
It's not directly possible to reverse sort an array of primitives (i.e., int[] arr = {1, 2, 3};
) using Arrays.sort()
and Collections.reverseOrder()
because those methods require reference types (Integer
) instead of primitive types (int
).
However, we can use Java 8 Stream to first box the array to sort in reverse order:
// an array of ints
int[] arr = {1, 2, 3, 4, 5, 6};
// an array of reverse sorted ints
int[] arrDesc = Arrays.stream(arr).boxed()
.sorted(Collections.reverseOrder())
.mapToInt(Integer::intValue)
.toArray();
System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]
Java 8:
Arrays.sort(list, comparator.reversed());
Update:
reversed()
reverses the specified comparator. Usually, comparators order ascending, so this changes the order to descending.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With