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.
For primitive array types, you would have to write a reverse sort algorithm:
Alternatively, you can convert your
int[]toInteger[]and write a comparator:or use
Collections.reverseOrder()since it only works on non-primitive array types.and finally,