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

Sorting of hashtables in a vector

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am looking at some code which sets a collection of hashtables (which should have been beans), into a vector, and that vector being moved around. Now there is a need to make sure the hashtables in the vector are sorted, based on any parametric value that is a part of the keys of the hashtable. I am trying to revisit some code and was able to come up with this,havent been able to test it though, I had read somewhere that comparing the hashcode values should help. I am not sure if the below is correct. Would this be the best approach ? Could you please help me with this one ?




cheers,
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that wouldn't be the best approach. You are ordering the entries by their hashcodes if you do that, and the hashcodes are unpredictable random numbers. Which I don't think you want to use to sequence them.

If you want to sort on this "parametric value", whatever that is, then get the "parametric values" for each of the entries and just return "a.compareTo(b)" in your Comparator's method.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input.

The only part that I am not able to understand is, how the sort method (with the comparator being passed) spin the loop ?
For ex: I did this :


For ex. here, I get the key based on the enumeration I retrieve. But how would that happen in the above sort ?



Id be glad for any pointers.

cheers,
 
Let me tell you a story about a man named Jed. He made this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic