• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to sort a map descendingly

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic