359

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 :[

I Like to Code
  • 7,101
  • 13
  • 38
  • 48
AFK
  • 4,333
  • 4
  • 23
  • 22
  • Take a look at the [usages of Collections.reverseOrder](https://www.codota.com/code/java/methods/java.util.Collections/reverseOrder) – drorw Jun 04 '18 at 15:42
  • 6
    A lot of below solutions will work on Integer and not on int type (make sure you are using appropriate) – Manish Jain Mar 06 '21 at 22:01

29 Answers29

431

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.

Rifat
  • 3
  • 2
  • 175
    It cannot sort arrays of primitives – Masood_mj Jul 31 '12 at 01:24
  • 15
    Convert your primitives to their respective objects. Integer for int, Double for double, Boolean for boolean, etc. – Ishmael Aug 21 '13 at 15:32
  • 14
    if you still want to use your custom [comparator](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html): `Collections.reverseOrder(this)` – Sebastian Hojas Oct 23 '13 at 14:24
  • Collections.reverseOrder() takes no parameters (unless I'm missing something?), instead I used myComparator.reversed(). – jsaven Apr 17 '17 at 11:36
  • I use this (example): Comparator notificationDataComparator = Collections.reverseOrder(GenericComparator .createComparator(AdMessage.CREATED_AT_COMPARATOR)); Collections.sort(allMessageList, notificationDataComparator); – Alexei Sep 22 '18 at 14:20
  • Why so much trouble, just Arrays.sort() and then loop from back to start ? Passing all primitives to objects seems really inefficient. – MissingSemiColon Oct 22 '19 at 07:21
  • In addition to the problem that you have to convert to Integer and not int is that if you have at least a null in that array you'll get a `NullPointerException` – ACV Sep 11 '20 at 19:38
114

for a list

Collections.sort(list, Collections.reverseOrder());

for an array

Arrays.sort(array, Collections.reverseOrder());
Tyilo
  • 28,998
  • 40
  • 113
  • 198
Ornithopter
  • 2,020
  • 2
  • 17
  • 16
  • 68
    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)" – Dixit Singla Jul 09 '14 at 10:12
  • 14
    int is not an Object. Try use Integer[] instead. – Ornithopter Oct 10 '14 at 04:04
  • 6
    int is a primary type while Integer is not. That's why Integer has methods like parse, toString, etc. – Ornithopter Mar 17 '15 at 04:50
90

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).

River
  • 8,585
  • 14
  • 54
  • 67
William
  • 1,101
  • 6
  • 11
84

an alternative could be (for numbers!!!)

  1. multiply the Array by -1
  2. sort
  3. multiply once again with -1

Literally spoken:

array = -Arrays.sort(-array)
arserbin3
  • 6,010
  • 8
  • 36
  • 52
FHDougherty
  • 911
  • 6
  • 2
  • 11
    This method is actually creative if we are sorting numbers, even though it is not generic and could cause problems for overflow... – hackjutsu May 28 '15 at 23:31
  • 7
    This is very good answer for primitive types. You are genius. – Halil İbrahim Oymacı Oct 27 '16 at 20:18
  • 4
    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. – Andreas Nov 01 '16 at 23:33
  • 2
    @Halil İbrahim Oymacı: -array syntax doesn't work for me: "bad operand type int[] for unary operator '-'" – Line Apr 08 '17 at 08:25
  • 9
    @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. – Halil İbrahim Oymacı Apr 16 '17 at 11:03
54

without explicit comparator:

Collections.sort(list, Collections.reverseOrder());

with explicit comparator:

Collections.sort(list, Collections.reverseOrder(new Comparator()));
Ram
  • 3,092
  • 10
  • 40
  • 56
Milan
  • 693
  • 5
  • 4
26

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]
kimbaudi
  • 13,655
  • 9
  • 62
  • 74
  • 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. – Owen Feb 22 '23 at 17:03
  • this was great. thanks for your help, but i hate java now lol – JBoothUA Feb 26 '23 at 04:23
16

First you need to sort your array using:

Collections.sort(myArray);

Then you need to reverse the order from ascending to descending using:

Collections.reverse(myArray);
mcvkr
  • 3,209
  • 6
  • 38
  • 63
Masoud
  • 261
  • 3
  • 4
9

Java 8:

Arrays.sort(list, comparator.reversed());

Update: reversed() reverses the specified comparator. Usually, comparators order ascending, so this changes the order to descending.

Michel Jung
  • 2,966
  • 6
  • 31
  • 51
  • 4
    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. – Ruslan Skaldin Mar 19 '19 at 05:06
5

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:

ArrayUtils.reverse(array);
Josip Maslac
  • 270
  • 3
  • 8
  • 1
    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? – Betlista Apr 17 '14 at 16:04
  • 1
    And that one step being? – Josip Maslac Apr 18 '14 at 14:17
  • 5
    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);` – Josip Maslac Apr 22 '14 at 14:27
5

When an array is a type of Integer class then you can use below:

Integer[] arr = {7, 10, 4, 3, 20, 15};
Arrays.sort(arr, Collections.reverseOrder());

When an array is a type of int data type then you can use below:

int[] arr = {7, 10, 4, 3, 20, 15};
int[] reverseArr = IntStream.rangeClosed(1, arr.length).map(i -> arr[arr.length-i]).toArray();
alkesh Miyani
  • 133
  • 1
  • 5
4

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.

Ken
  • 30,811
  • 34
  • 116
  • 155
4

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) <= 1st: 0, 1st-last: nums.length - 1
        // swap(2nd, 2nd-last) <= 2nd: i++,  2nd-last: j--
        // swap(3rd, 3rd-last) <= 3rd: i++,  3rd-last: j--
        //
        for (int i = 0, j = nums.length - 1, tmp; i < j; i++, j--) {
            tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }

        // dump the array (for Java 4/5/6/7/8/9)
        for (int i = 0; i < nums.length; i++) {
            System.out.println("nums[" + i + "] = " + nums[i]);
        }
    }
}

Output:

nums[0] = 9
nums[1] = 8
nums[2] = 7
nums[3] = 6
nums[4] = 5
nums[5] = 4
nums[6] = 3
nums[7] = 2
nums[8] = 1
nums[9] = 0
蔡宗容
  • 927
  • 12
  • 9
4

For 2D arrays to sort in descending order you can just flip the positions of the parameters

int[][] array= {
    {1, 5},
    {13, 1},
    {12, 100},
    {12, 85} 
};
Arrays.sort(array, (a, b) -> Integer.compare(a[1], b[1])); // for ascending order
Arrays.sort(array, (b, a) -> Integer.compare(a[1], b[1])); // for descending order

Output for descending

12, 100
12, 85
1, 5
13, 1
Magho
  • 334
  • 2
  • 6
3

Another solution is that if you're making use of the Comparable interface you can switch the output values which you had specified in your compareTo(Object bCompared).

For Example :

public int compareTo(freq arg0) 
{
    int ret=0;
    if(this.magnitude>arg0.magnitude)
        ret= 1;
    else if (this.magnitude==arg0.magnitude)
        ret= 0;
    else if (this.magnitude<arg0.magnitude)
        ret= -1;
    return ret;
}

Where magnitude is an attribute with datatype double in my program. This was sorting my defined class freq in reverse order by it's magnitude. So in order to correct that, you switch the values returned by the < and >. This gives you the following :

public int compareTo(freq arg0) 
{
    int ret=0;
    if(this.magnitude>arg0.magnitude)
        ret= -1;
    else if (this.magnitude==arg0.magnitude)
        ret= 0;
    else if (this.magnitude<arg0.magnitude)
        ret= 1;
    return ret;
}

To make use of this compareTo, we simply call Arrays.sort(mFreq) which will give you the sorted array freq [] mFreq.

The beauty (in my opinion) of this solution is that it can be used to sort user defined classes, and even more than that sort them by a specific attribute. If implementation of a Comparable interface sounds daunting to you, I'd encourage you not to think that way, it actually isn't. This link on how to implement comparable made things much easier for me. Hoping persons can make use of this solution, and that your joy will even be comparable to mine.

Community
  • 1
  • 1
Chris - Jr
  • 398
  • 4
  • 11
3

Adding my answer in here for a couple of different scenarios For an Array

Arrays.sort(a, Comparator.reverseOrder());

FWIW Lists

Lists.reverse(a);

Any and all Collections

Collections.reverse(a);
Rhett Harrison
  • 430
  • 4
  • 19
1

You could use stream operations (Collections.stream()) with Comparator.reverseOrder().

For example, say you have this collection:

List<String> items = new ArrayList<>();
items.add("item01");
items.add("item02");
items.add("item03");
items.add("item04");
items.add("item04");

To print the items in their "natural" order you could use the sorted() method (or leave it out and get the same result):

items.stream()
     .sorted()
     .forEach(item -> System.out.println(item));

Or to print them in descending (reverse) order, you could use the sorted method that takes a Comparator and reverse the order:

items.stream()
     .sorted(Comparator.reverseOrder())
     .forEach(item -> System.out.println(item));

Note this requires the collection to have implemented Comparable (as do Integer, String, etc.).

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
1

There is a lot of mess going on here - people suggest solutions for non-primitive values, try to implement some sorting algos from the ground, give solutions involving additional libraries, showing off some hacky ones etc. The answer to the original question is 50/50. For those who just want to copy/paste:

// our initial int[] array containing primitives
int[] arrOfPrimitives = new int[]{1,2,3,4,5,6};

// we have to convert it into array of Objects, using java's boxing
Integer[] arrOfObjects = new Integer[arrOfPrimitives.length];
for (int i = 0; i < arrOfPrimitives.length; i++) 
    arrOfObjects[i] = new Integer(arrOfPrimitives[i]);

// now when we have an array of Objects we can use that nice built-in method
Arrays.sort(arrOfObjects, Collections.reverseOrder());

arrOfObjects is {6,5,4,3,2,1} now. If you have an array of something other than ints - use the corresponding object instead of Integer.

curveball
  • 4,320
  • 15
  • 39
  • 49
1

Simple method to sort an int array descending:

private static int[] descendingArray(int[] array) {
    Arrays.sort(array);
    int[] descArray = new int[array.length];
    for(int i=0; i<array.length; i++) {
        descArray[i] = array[(array.length-1)-i];
    }
    return descArray;
}
Will
  • 1,487
  • 1
  • 23
  • 35
1

Here is how I sorted a primitive type int array.

int[] intArr = new int[] {9,4,1,7};
Arrays.sort(nums);
Collections.reverse(Arrays.asList(nums));

Result:

[1, 4, 7, 9]
Rahul
  • 543
  • 1
  • 7
  • 24
1

NOTE: this is a N log N time complexity but easier to read through and understand how to perform sort in reverse.

credits to Ken for suggestion of this solution.

    // this func sorts in n log n time complexity
    public void sort_reverse(int[] arr) {
        // 1. sort the arr in asc order
        Arrays.sort(arr);
        // 2. now sort all values in descending order
        for (int i = 0, j = arr.length - 1; i < arr.length / 2;i++) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            j--;
        }
    }
0

I know that this is a quite old thread, but here is an updated version for Integers and Java 8:

Arrays.sort(array, (o1, o2) -> o2 - o1);

Note that it is "o1 - o2" for the normal ascending order (or Comparator.comparingInt()).

This also works for any other kinds of Objects. Say:

Arrays.sort(array, (o1, o2) -> o2.getValue() - o1.getValue());
sebschaef
  • 226
  • 1
  • 8
0

This worked for me:

package doublearraysort;

import java.util.Arrays;
import java.util.Collections;

public class Gpa {


    public static void main(String[] args) {
        // initializing unsorted double array
        Double[] dArr = new Double[] {                 
            new Double(3.2),
            new Double(1.2),
            new Double(4.7),
            new Double(3.3),
            new Double(4.6),
           };
        // print all the elements available in list
        for (double number : dArr) {
            System.out.println("GPA = " + number);
        }

        // sorting the array
        Arrays.sort(dArr, Collections.reverseOrder());

        // print all the elements available in list again
        System.out.println("The sorted GPA Scores are:");
        for (double number : dArr) {
            System.out.println("GPA = " + number);
        }
    }
}

Output:

GPA = 3.2
GPA = 1.2
GPA = 4.7
GPA = 3.3
GPA = 4.6
The sorted GPA Scores are:
GPA = 4.7
GPA = 4.6
GPA = 3.3
GPA = 3.2
GPA = 1.2
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
0
public double[] sortArrayAlgorithm(double[] array) { //sort in descending order
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            if (array[i] >= array[j]) {
                double x = array[i];
                array[i] = array[j];
                array[j] = x;
            }
        }
    }
    return array;
}

just use this method to sort an array of type double in descending order, you can use it to sort arrays of any other types(like int, float, and etc) just by changing the "return type", the "argument type" and the variable "x" type to the corresponding type. you can also change ">=" to "<=" in the if condition to make the order ascending.

Mohsen Mousavi
  • 131
  • 2
  • 8
0

Another way with Comparator

import java.util.Arrays;
import java.util.Comparator;
...

Integer[] aInt = {6,2,3,4,1,5,7,8,9,10};
Arrays.sort(aInt, Comparator.reverseOrder()  );
zemiak
  • 361
  • 2
  • 5
0

It's good sometimes we practice over an example, here is a full one:

sortdesc.java

import java.util.Arrays;
import java.util.Collections;
class sortdesc{
public static void main(String[] args){
       // int Array
       Integer[] intArray=new Integer[]{
                 new Integer(15),
                 new Integer(9),
                 new Integer(16),
                 new Integer(2),
                 new Integer(30)};

       // Sorting int Array in descending order
       Arrays.sort(intArray,Collections.reverseOrder());

       // Displaying elements of int Array
       System.out.println("Int Array Elements in reverse order:");
       for(int i=0;i<intArray.length;i++)
          System.out.println(intArray[i]);

       // String Array
       String[] stringArray=new String[]{"FF","PP","AA","OO","DD"};

       // Sorting String Array in descending order
       Arrays.sort(stringArray,Collections.reverseOrder());

       // Displaying elements of String Array
       System.out.println("String Array Elements in reverse order:");
       for(int i=0;i<stringArray.length;i++)
          System.out.println(stringArray[i]);}}

compiling it...

javac sortdec.java

calling it...

java sortdesc

OUTPUT

Int Array Elements in reverse order:
30
16
15
9
2
String Array Elements in reverse order:
PP
OO
FF
DD
AA

If you want to try an alphanumeric array...

//replace this line:
String[] stringArray=new String[]{"FF","PP","AA","OO","DD"};

//with this:
String[] stringArray=new String[]{"10FF","20AA","50AA"};

you gonna get the OUTPUT as follow:

50AA
20AA
10FF

source

PYK
  • 3,674
  • 29
  • 17
0

There is a way that might be a little bit longer, but it works fine. This is a method to sort an int array descendingly.

Hope that this will help someone ,,, some day:

public static int[] sortArray (int[] array) {
    int [] sortedArray = new int[array.length];
    for (int i = 0; i < sortedArray.length; i++) {
        sortedArray[i] = array[i];
    }
    
    boolean flag = true;
    int temp;
    while (flag) {
        flag = false;
        for (int i = 0; i < sortedArray.length - 1; i++) {
            if(sortedArray[i] < sortedArray[i+1]) {
                temp = sortedArray[i];
                sortedArray[i] = sortedArray[i+1];
                sortedArray[i+1] = temp;
                flag = true;
            }
        }
    }
    
    return sortedArray;
    
}
Maged Almaweri
  • 312
  • 4
  • 11
0

I had the below working solution

    public static int[] sortArrayDesc(int[] intArray){
    Arrays.sort(intArray);                      //sort intArray in Asc order
    int[] sortedArray = new int[intArray.length];   //this array will hold the sorted values

    int indexSortedArray = 0;
    for(int i=intArray.length-1 ; i >= 0 ; i--){    //insert to sortedArray in reverse order
        sortedArray[indexSortedArray ++] = intArray [i];
    }
    return sortedArray;
}
sher17
  • 607
  • 1
  • 12
  • 31
0

Arrays.sort(nums, Collections.reverseOrder());

But Arrays.sort() will not work with primitive objects like int[]. For them it will throw,

error: no suitable method found for sort(int[],Comparator)

Arrays.sort() will work with primitive objects only in increasing order.

Better to convert into a collection and then sort

Collections.sort(Arrays.asList(nums), Collections.reverseOrder())

Prashant
  • 105
  • 1
  • 2
  • 8
-2

I know many answers are here, but still thinks , none of them tried using core java. And using collection api , you will end up wasting so much memory and reseduals.

here is a try with pure core concepts , and yes this may be better way if you are more concerned about memory footprints.

    int[] elements = new int [] {10,999,999,-58,548,145,255,889,1,1,4,5555,0,-1,-52};
    //int[] elements = null;
    
    if(elements != null && elements.length >1)
    {
        int max = 0, index = 0;
        for(int i =0;i<elements.length;i++)//find out what is Max
        {
            if(elements[i] > max)
                {
                    max = elements[i];
                    index = i;
                }
        }
        elements[index] = elements[0];//Swap the places
        elements[0] = max;
        for(int i =0;i < elements.length;i++)//loop over element
        {
            for(int j = i+1;j < elements.length;j++)//loop to compare the elements
            {
                if(elements[j] > elements[i])
                {
                    max = elements[j];
                    elements[j] = elements[i];
                    elements[i] = max;
                }
            }
        }
        
    }//i ended up using three loops and 2 extra variables
    System.out.println(Arrays.toString(elements));//if null it will print null
    // still love to learn more, please advise if we can do it better.

Love to learn from you too !

CuriousDev
  • 400
  • 3
  • 11