Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting hashmap values

 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cut and paste error -- you're comparing o1 to o1, not to o2!
[ October 13, 2003: Message edited by: Ernest Friedman-Hill ]
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic