aspose file tools*
The moose likes Java in General and the fly likes Question about Sorting with Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about Sorting with Hashtable" Watch "Question about Sorting with Hashtable" New topic
Author

Question about Sorting with Hashtable

Manimekala Velautham
Greenhorn

Joined: May 31, 2006
Posts: 16
Hi

I want to sort a Hashtable's values in alphabetical,with duplicates and in case_insensitive manner. I tried the following code. Please take a look.



With the above code I got the values in the Hashtable alphabetically,with duplicates and in case_insensitive manner. Now I want to have corresponding keys also. How to do this?

Any help is appreciated.

Thanks
Manimekala
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Sounds like you want a SortedMap, not a Hashtable (*). There is an implementation of SortedMap in the API; look up TreeMap.

(*) Note that you almost never actually want a Hashtable. That's an old, obsolete class. Where you might have used Hashtable, you should generally use HashMap.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
If I understand the OP correctly, he wants the keys in the order specified by the sorted values. If that's right, then a SortedMap won't work, as that is sorted by the keys.

As Peter says, it is better to use a HashMap. You can retrieve a set of Map.Entry objects from this using the entrySet method. These objects contain the key and value. You will then need to write a Comparator to sort these and pass an instance of this Comparator to the Collections.sort method.

Your Comparator will be pretty simple. In the overridden compare method, simply use the Map.Entry.getValue method to retrieve the values and compare them using the String.compareToIgnoreCase method.

Of course, if Peter's interpretation of your question was correct, just ignore this post.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Sorting with Hashtable