• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting Array in Descending Order  RSS feed

 
ShaikSalam Bashoeb
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to sort arrays in ascending order by using API's sort method, I want to know how to sort arrays in descending order by using API's method. For your information, i want to mention that i know how to sort arrays in descending order through manually.

Any suggestions will be appreciated.
Thanks in Advanced.........
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Comparator which returns negative numbers for "greater" and positive numbers for "smaller."

Sort in the usual fashion and reverse the array.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have a look at the reverseOrder() and reverseOrder(Comparator) methods on the Collections class.

They allow you to create a reverse order comparator based upon either the natural order of the objects or the supplied comparator depending upon which you choose. Have a look at the Collections java docs for more info.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!