This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes sorting hashmap values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "sorting hashmap values" Watch "sorting hashmap values" New topic
Author

sorting hashmap values

shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Hi,
I need a sort a few float values but at the same time keep track of their ids. So i use a hashmap and store id,value pairs. I tried sorting this way :

Is my approach correct?? I only get one element in the iterator eventually wherease my hashmap has around 800 entrries.. Whats wrong with this code. Thanx,
shankar
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34


Cut and paste error -- you're comparing o1 to o1, not to o2!
[ October 13, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
Originally posted by Ernest Friedman-Hill:
[code]
public int compare(Object o1, Object o2) {
Float f1 = (Float)((Map.Entry)o1).getValue();
Float f2 = (Float)((Map.Entry)o1).getValue();
typo! **********************************^^
return f1.compareTo(f2);
}
[code]
Cut and paste error -- you're comparing o1 to o1, not to o2!

huh, i spent the afternoon learning how to do this and eventually end up making such silly mistakes. thanx a lot anyways, shud have been more careful.it works now.
shankar
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: sorting hashmap values
 
Similar Threads
Hashtable iterator question
sorting
Sorting TreeMap with values
Sorting HashMap by values
How to sort a list of things (string, float)?