Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class?
Or do I have to stop being lazy and do this myself :[
arraysintjavasorting
Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class?
Or do I have to stop being lazy and do this myself :[
Best Answer
You could use this to sort all kind of Objects
Arrays.sort()
cannot be used directly to sort primitive arrays in descending order. If you try to call theArrays.sort()
method by passing reverse Comparator defined byCollections.reverseOrder()
, it will throw the errorThat 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.