43

Possible Duplicate:
Sort arrays of primitive types in descending order
Java : How to sort an array of floats in reverse order?
How do I reverse an int array in Java?

The following code will sort the array in ascending order :

int a[] = {30,7,9,20};
Arrays.sort(a);
System.out.println(Arrays.toString(a));

I need to sort it in descending order. How do I use Comparator to do this?

Please help.

android
  • 570
  • 1
  • 6
  • 13
  • I just created [a library for sorting primitive arrays with a custom comparator](https://github.com/mintern-java/primitive#java-primitive). The first "sample usage" is sorting an `int[]` in descending order. – Brandon Nov 23 '14 at 23:55
  • Just traverse the array in a descending order after sort. – Debjit Jun 21 '18 at 21:04

4 Answers4

22

For primitive array types, you would have to write a reverse sort algorithm:

Alternatively, you can convert your int[] to Integer[] and write a comparator:

public class IntegerComparator implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}

or use Collections.reverseOrder() since it only works on non-primitive array types.

and finally,

Integer[] a2 = convertPrimitiveArrayToBoxableTypeArray(a1);
Arrays.sort(a2, new IntegerComparator()); // OR
// Arrays.sort(a2, Collections.reverseOrder());

//Unbox the array to primitive type
a1 = convertBoxableTypeArrayToPrimitiveTypeArray(a2);
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 4
    error in convertPrimitiveArrayToBoxableTypeArray(a1) – android Sep 14 '11 at 10:06
  • @android, that method **never** exists, it's an example I created to do the conversion from `int[]` to `Integer[]`. – Buhake Sindi Sep 14 '11 at 10:18
  • If i convert from int to Integer,then I can use Collections.reverseOrder but i have to conver again to int.Is this efficient?I think its better just to reverse the array after Aarrays.sort() then two conversion – android Sep 14 '11 at 10:25
  • 1
    @android, `Arrays.sort()` doesn't provide a facility to use a `Comparator` on the primitive array types, hence why the 2 conversion. – Buhake Sindi Sep 14 '11 at 11:11
8

If it's not a big/long array just mirror it:

for( int i = 0; i < arr.length/2; ++i ) 
{ 
  temp = arr[i]; 
  arr[i] = arr[arr.length - i - 1]; 
  arr[arr.length - i - 1] = temp; 
}
Matjaz Muhic
  • 5,328
  • 2
  • 16
  • 34
7
    Comparator<Integer> comparator = new Comparator<Integer>() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2.compareTo(o1);
        }
    };

    // option 1
    Integer[] array = new Integer[] { 1, 24, 4, 4, 345 };
    Arrays.sort(array, comparator);

    // option 2
    int[] array2 = new int[] { 1, 24, 4, 4, 345 };
    List<Integer>list = Ints.asList(array2);
    Collections.sort(list, comparator);
    array2 = Ints.toArray(list);
John B
  • 32,493
  • 6
  • 77
  • 98
  • 1
    It seems the `Ints` class is in external google lib, `com.google.common.primitives.Ints`. – mazend Jan 07 '21 at 12:36
6

Guava has a method Ints.asList() for creating a List<Integer> backed by an int[] array. You can use this with Collections.sort to apply the Comparator to the underlying array.

List<Integer> integersList = Ints.asList(arr);
Collections.sort(integersList, Collections.reverseOrder());

Note that the latter is a live list backed by the actual array, so it should be pretty efficient.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588