Java Array Sort descending?
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.
Convert your primitives to their respective objects. Integer for int, Double for double, Boolean for boolean, etc.
Collections.reverseOrder() takes no parameters (unless I’m missing something?), instead I used myComparator.reversed().
I use this (example): Comparator
Collections.sort(list, Collections.reverseOrder());
Arrays.sort(array, Collections.reverseOrder());
int []array = <2,4,3,6,8,7>; Arrays.sort(array, Collections.reverseOrder()); is giving me an error! Error is: «The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], Comparator)»2,4,3,6,8,7>
int is a primary type while Integer is not. That’s why Integer has methods like parse, toString, etc.
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. )
This method is actually creative if we are sorting numbers, even though it is not generic and could cause problems for overflow.
Except that it’ll fail for Integer.MIN_VALUE (or whichever primitive is used). Would be better to sort() , then reverse() , but you’ll have to do the reversing yourself, since they didn’t add Arrays.reverse() implementations.
@Halil İbrahim Oymacı: -array syntax doesn’t work for me: «bad operand type int[] for unary operator ‘-‘»
@line You must multiple -1 to array. Above code is pseudo code. You can multiple -1 to array in a for loop then call Array.sort() method, lastly you multiple -1 to array again.
without explicit comparator:
Collections.sort(list, Collections.reverseOrder());
Collections.sort(list, Collections.reverseOrder(new Comparator()));
It’s not directly possible to reverse sort an array of primitives (i.e., int[] arr = ; ) 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 = ; // 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]
I’m assuming the time complexity is still O(nlgn) since streaming the values and mapping them are done on the same «level» (meaning the time complexity of this whole code is something like (n + nlgn + n)? But please correct me if I’m wrong.
First you need to sort your array using:
Then you need to reverse the order from ascending to descending using:
Collections.reverse(myArray);
Arrays.sort(list, comparator.reversed());
Update: reversed() reverses the specified comparator. Usually, comparators order ascending, so this changes the order to descending.
It’s work perfectly with Objects but not with primitives. For sort primitive int, you should sort in ASC order and then reverse the answer.
When an array is a type of Integer class then you can use below:
Integer[] arr = ; Arrays.sort(arr, Collections.reverseOrder());
When an array is a type of int data type then you can use below:
int[] arr = ; int[] reverseArr = IntStream.rangeClosed(1, arr.length).map(i -> arr[arr.length-i]).toArray();
For array which contains elements of primitives if there is org.apache.commons.lang(3) at disposal easy way to reverse array (after sorting it) is to use:
Why to sort it first in ascending order and then use external library to revert this order, when it can be done in one step?
Yes but (as stated in the comments to those answers) that doesn’t work for primitives which my answer address. Ofcourse my answer is certainly not the optimal one but I found it to meet the criteria of being «easy» which the original author emphasized — ie. Arrays.sort(primitives); ArrayUtils.reverse(primitives);
I don’t know what your use case was, however in addition to other answers here another (lazy) option is to still sort in ascending order as you indicate but then iterate in reverse order instead.
For discussions above, here is an easy example to sort the primitive arrays in descending order.
import java.util.Arrays; public class Main < public static void main(String[] args) < int[] nums = < 5, 4, 1, 2, 9, 7, 3, 8, 6, 0 >; Arrays.sort(nums); // reverse the array, just like dumping the array! // swap(1st, 1st-last) // dump the array (for Java 4/5/6/7/8/9) for (int i = 0; i < nums.length; i++) < System.out.println("nums[" + i + "] mt24">)" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f4.0%2f" data-se-share-sheet-license-name="CC BY-SA 4.0" data-s-popover-placement="bottom-start">Share)" title="">Improve this answeranswered Oct 10, 2019 at 5:51蔡宗容蔡宗容907 12 silver badges 9 bronze badges