• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting HashMap

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i have a hashmap which has bunch of person objects with personId as key
pr = new Person("C1","Rashid","Darvesh");
prHM.put(pr.getPersonid(),pr);
pr=new Person("C2","Billy","Park");
prHM.put(pr.getPersonid(),pr);
pr = new Person("C3","Simpson","Sung");
prHM.put(pr.getPersonid(),pr);
pr=new Person("C4","Josh","Yu");
prHM.put(pr.getPersonid(),pr);
pr = new Person("C5","Jon","Wollman");
prHM.put(pr.getPersonid(),pr);
pr=new Person("C6","Ken","Brown");
prHM.put(pr.getPersonid(),pr);
pr = new Person("C7","Elmar","Mamadov");
prHM.put(pr.getPersonid(),pr);
pr=new Person("C8","Jack","Larks");
prHM.put(pr.getPersonid(),pr);
pr = new Person("C9","Mary","Jane");
prHM.put(pr.getPersonid(),pr);
pr=new Person("C10","Matt","Landish");
prHM.put(pr.getPersonid(),pr);
I have a requirement to sort the map with either first name or lastname. i tried using treeMap, but no result yet. Can someone please let me know how i the best possible way to sort
Thanks
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashid,

What exactly happens when you use TreeMap? Doesn't it sort based on "natural ordering" of the Key, which seems to be of String type in your case?

You can write your output here for the testcase you have provided here so we can see what is expected and what you are getting.

Thanks
Maulin
[ November 08, 2004: Message edited by: Maulin Vasavada ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic