Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 http://www.j2ee.me/javase/6/docs/api/java/util/Arrays.html

?

Or do I have to stop being lazy and do this myself :[

share|improve this question
16  
"Or do I have to stop being lazy and do this myself " - as a programmer, you should never stop being lazy ;)) – luvieere Nov 7 '09 at 23:11
1  
Laziness is the cause of humankind's progress. As a programmer, a progressive occupation certainly, you should try to be as lazy as possible. Just be smart about it :) – David Miler Oct 15 '12 at 14:50

2 Answers

up vote 42 down vote accepted

You could use this

sort(T[] a, Comparator<? super T> c) 

Arrays.sort(a, Collections.reverseOrder());
share|improve this answer
6  
It cannot sort arrays of primitives – Masood_mj Jul 31 '12 at 1:24

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

share|improve this answer
   
The OP wants to sort an array. Collections.sort() takes a List as input parameter, not an array. – Pascal Thivent Nov 8 '09 at 2:22
ops,i wrote Collections instead of Arrays.Its corrected now. – William Nov 8 '09 at 5:12
+1 for explaining how to use your own comparator. – dj18 Jul 6 '12 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.