• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

sort a map on value

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I got a map and would like to sort it based on the values. So far, I'm attempting to create a TreeSort and getting ClassCastException. Any ideas anyone how to sort a map?
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

This post has been moved to a more appropriate forum.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the Collections class which has a bunch of utility methods for such operations on collections.

Collections.sort(mapValues) will sort the contents of mapValues
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get all the values from a Map, put them in a List, and sort them using Collections.sort(). However this has no effect on the original Map Using a TreeMap won't help much either, as a TreeMap wants to sort by key, not value. You could solve this problem using two maps, where the second maps in reverse order (reversing key and value). Or you could use a TreeBidimap from Jakarta Commons Collections. That last is probably easiest.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the values in your Map don't implement the Comparable interface. You could use the TreeSet Constructor that takes a Comparator object to do the sorting.
 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, I ended up using 2 maps. both maps have similar keys and different values. They get created in a loop simultaneously. Now, if I sort the first map, the second map will become useless. I think I'd have to use a different data structure for this.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic