• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort ArrayList of HashMap elements base on HashMap value  RSS feed

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to add elements of a HashMap to an ArrayList and then sort the ArrayList with Collections.sort based on the "value" of the HashMap element? Here is a simplified version of the code I am trying....



 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted, you didn't get the code tags quite right so I've fixed them for you. The code goes in between ONE pair of tags, not between two (empty) pairs.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a SortedMap interface with a TreeMap implementation in the standard JDK. Is that something which could be useful to you?
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Comparator that have a method like this:Then use an instance of this Comparator as a parameter to the sorting method.

Are you sure you want List<Map<String, Integer>>? This is a list of maps. I think you want a List<String>.

Or maybe you need a Jumper class with name and score properties?
 
Ted Schrey
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both... in the course I am taking, I've been exposed to the Comparator, so I think I'd try it with that... thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!