47

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.

2

4 Answers 4

24

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);
Sign up to request clarification or add additional context in comments.

4 Comments

error in convertPrimitiveArrayToBoxableTypeArray(a1)
@android, that method never exists, it's an example I created to do the conversion from int[] to Integer[].
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, Arrays.sort() doesn't provide a facility to use a Comparator on the primitive array types, hence why the 2 conversion.
7

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; 
}

3 Comments

Yes.I know that.Actually i want to know is there a way to use Comparator,
Not on primitives. Comparator has to be implemented because it's an interface.
If you're going to do a foreach with a mirror, I think a negative iterator with (int i = array.length - 1; i >= 0; i--) would be much simpler.
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);

1 Comment

It seems the Ints class is in external google lib, com.google.common.primitives.Ints.
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.

4 Comments

Yes.I know that.Actually i want to know is there a way to use Comparator.
@android, no Comparator works on Objects, not primitives.
My eclipse showing error on ArrayUtils
@android forget ArrayUtils, use Guava

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.