wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to sort a map descendingly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to sort a map descendingly" Watch "How to sort a map descendingly" New topic
Author

How to sort a map descendingly

narender sunkam
Ranch Hand

Joined: May 01, 2004
Posts: 30
Hi,

I have created a map object that stores the key and the value, but it has to sort the values by decesinding order,I was thinking of making a treemap to sort it in accesinding order.Can some one please help me to solve this with a small piece of code.I dont know much on the subject of sorting and searching so any help would be greatly appericated.

Thanks and regards
Narender


narender kumar
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
You will have to give some more information about what you are trying to accomplish. Usually you put something in a map to be able to retrieve it based on the key.
You can also use the values() method of the Map to retrieve the values and sort them how you need them.
narender sunkam
Ranch Hand

Joined: May 01, 2004
Posts: 30
Thanks for quick reply bart zagers,
actually I am trying to sort the map on key.
please help me with a small piece of code.

thanks and regards
Narender
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262

I have created a map object that stores the key and the value, but it has to sort the values by descending order


How frequently do you need this sorted representation of your data?

If you need a sorted representation very infrequently, a TreeMap might be overkill ... because add/remove methods for TreeMap run in O(lgn), which is not as good as the O(1) run time for HashMap's add/remove methods.

On the other hand, if you need to operate on the elements in a sorted fashion frequently, then you'll want to avoid paying O(n lg n) to obtain a sorted representation ... so maintaining a sorted structure could be advantageous and TreeMap might be a good candidate.


Fun programming etcetera!
narender sunkam
Ranch Hand

Joined: May 01, 2004
Posts: 30
thanks for reply Dave Wingate ,

How frequently do you need this sorted representation of your data?


I need this to sort descendingly only once.And I am not going to remove or add on this.

thanks and regards
narender
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
These should be useful.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#reverseOrder()
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)



http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to sort a map descendingly
 
Similar Threads
Sorting data table problem
Code to sort and remove duplicates in ArrayList and Hashmap
Why is this called Meaningless Drivel?
Looping through a set of data
problem with frame size