• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting a Hashtable by values & retriving kay-value pair

 
AMIT ARADHYE
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All Professionals,
I want to sort two diff. Hashtables by "values".
First Hashtable contains key(String type)- value(String) pairs.
Second Hashtable contains key(String type)- value(Integer) pairs.
FYI I am populating them as...
first.put("20030120",new Integer (56));
first.put("20030118",new Integer (19));
first.put("20030125",new Integer (25));
first.put("20030122",new Integer (32));
first.put("20030117",new Integer (67));
first.put("20030123",new Integer (34));
first.put("20030124",new Integer (42));
first.put("20030121",new Integer (19));
first.put("20030119",new Integer (98));
other ...like this....
second.put("chetooo105","44");
second.put("19tandf127","1");
second.put("18januah140","3");
second.put("Guest79","1");
second.put("18janrout115","1");
second.put("21jantandfnew2152","10");
second.put("19janetailer118","4");
second.put("Au1410113","10");
Can any one guide me to sort these both by values and retrive a kay-value pair from sorted entity?
Thanks
 
Gopi Balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,
You could use an implementation of the SortedMap interface - a TreeMap.
But, the sort is based on keys, not on values. Have a look at the Javadoc for SortedMap and TreeMap.
-GB.
 
AMIT ARADHYE
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello GB,
I am pretty comfortable with Java Collections Framework. And hence very much aware of the mechanism to "sort by keys" . It's my specific requirement to get that sorted by values where I am currently stuck-up.And so is 'specific' posting.
Any how ..thanks for kind, but not useful suggestion.
Amit
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you meant "Map" when you said "Hashtable", or I'd have to question whether you were "comfortable with Java Collections Framework"
One thing I don't understand about your question is... do you want to relate these two Maps in some way? If so, how? For the moment, I will assume not.
In any case, it's not very difficult. As you know, Maps either have no sorting order, or sort by key value, so you will have to use a second Collection. As you want to extract key/value pairs from this second (sorted) Collection, it will have to be a Collection of Map.Entry objects with a suitable sorting order. EgWhere a suitable comparator could beNote that it compares the keys as well as the values --- this is necessary to be consistent with Map.Entry.equals() and to ensure that our entrySet behaves like a Set.
HTH
- Peter
[ January 27, 2003: Message edited by: Peter den Haan ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shame to waste it:
 
AMIT ARADHYE
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..both Peter & Barry.
Both the alternatives are workable and satisfies my requirement. But Barry's is more effective and gives predictable results than Peters. The reason behind this is ..
Barry is comparing two objects in 'Integer' form (which in my case is specific requirement) where as Peter is opting for 'Object' form.
When I pass variety of data combinations to these both codes.....more realiable is Barry's one.
ANy how ...thanks a lot ....both of you...Peter & Barry.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by AMIT ARADHYE:
Both the alternatives are workable and satisfies my requirement. But Barry's is more effective and gives predictable results than Peters. The reason behind this is ..
Barry is comparing two objects in 'Integer' form (which in my case is specific requirement) where as Peter is opting for 'Object' form.
Predictability has nothing whatsoever to do with it. Comparable is a standard Java interface with perfectly clear semantics, implemented by all the Java wrapper types, and the sorting order of Comparable objects is both well-defined and predictable. In fact, both Comparators call the very same Integer.compareTo() method.
If you regard writing a new Comparator for every single data type you want to compare as more effective than one Comparator which handles everything then, well, there's no accounting for taste I guess
- Peter
[ January 28, 2003: Message edited by: Peter den Haan ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit, I fully agree with Peter, if you just want use my method for just a couple of HashMaps ok, but for a more general solution use Peter's code. It's called the "generic" algorithmic approach and will pay off in the long run.
-Barry
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry, but your code is more transparent (which is also evident from Amit's response) and thereby perhaps more appropriate here.
- Peter
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So is it time for a group hug here?
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HUG!!!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other significant difference between the two code examples is whether to use a TreeSet or Collections.sort(). I'm pretty sure that for a one-time sort Collections.sort() is more efficient (as well as slightly more readable), while TreeSet is more appropriate if you plan on modifying the List again after the initial sort, and need it to remain sorted. You don't want to call sort() after every little modification.
[ January 28, 2003: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic