Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

sorting int[] in descending order

Thread view: 
Vahe Musoyan - 27 Oct 2006 16:05 GMT
What's the best way to sort an array of integers in descending order?
I know that one can sort in ascending order and reverse the array. Is
there any other/better way?
Patricia Shanahan - 27 Oct 2006 16:20 GMT
> What's the best way to sort an array of integers in descending order?
> I know that one can sort in ascending order and reverse the array. Is
> there any other/better way?

There are other ways, but I don't think any of the ones I know are better:

1. Write your own quicksort descending order implementation.

2. Create an Integer array with the values from the int[] and
Arrays.sort it using a supplied Comparator that reverses the
a.compareTo(b) result.

3. Replace each element of the array with the result of subtracting it
from -1, sort normally, then repeat the replacement operation. The
initial subtraction maps the largest value to the smallest, smallest to
largest etc. The final subtraction restores the original values.

Option 1 involves a lot of coding. Options 2 and 3 each involve more
overhead than reversing the array after an ascending order sort.

If, for its other uses, this array is on the borderline between being
best represented by an int[] or an Integer[], then the descending sort
requirement might tip it over to Integer[], and you could do the sort
with descending order Comparator without any copying.

Patricia
Eric Sosman - 27 Oct 2006 16:37 GMT
Patricia Shanahan wrote On 10/27/06 11:20,:

>>What's the best way to sort an array of integers in descending order?
>>I know that one can sort in ascending order and reverse the array. Is
[quoted text clipped - 13 lines]
> largest etc. The final subtraction restores the original values.
> [...]

4. "Count backwards:" sort the array in ascending order, but
"reflect" the index whenever you access it.  Most crudely,
replace array[i] with array[array.length-1-i].  Somewhat more
smoothly, replace

    for (int i = 0;  i < array.length;  ++i)

with

    for (int i = array.length;  --i >= 0;  )

Signature

Eric.Sosman@sun.com

Daniel Dyer - 28 Oct 2006 18:35 GMT
> 2. Create an Integer array with the values from the int[] and
> Arrays.sort it using a supplied Comparator that reverses the
> a.compareTo(b) result.

If you choose to use this method you can make use of  
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#reverseOrder().

Dan.

Signature

Daniel Dyer
http://www.uncommons.org



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.