This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sorting a Hashtable by values & retriving kay-value pair Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting a Hashtable by values & retriving kay-value pair" Watch "Sorting a Hashtable by values & retriving kay-value pair" New topic
Author

Sorting a Hashtable by values & retriving kay-value pair

AMIT ARADHYE
Greenhorn

Joined: Dec 13, 2000
Posts: 15
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

Joined: Jan 23, 2003
Posts: 84
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

Joined: Dec 13, 2000
Posts: 15
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 03, 2002
Posts: 7729
Shame to waste it:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
AMIT ARADHYE
Greenhorn

Joined: Dec 13, 2000
Posts: 15
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Sep 29, 2000
Posts: 8521

So is it time for a group hug here?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
HUG!!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting a Hashtable by values & retriving kay-value pair
 
Similar Threads
Sorting Hashtable by values & retriving kay-value pair
Sorting HashMap by values
Sort Hashtable by Values & retrive key-value pair
sorting
how to compare elements of List