File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort a map descendingly

 
narender sunkam
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
bart zagers
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
narender sunkam
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic