• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

sorting hashmap values

 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic