Re: How can I sort a ENTIRE PAIR key/value, sort by the value, in a Hashtable?
Posted By:
Anonymous
Posted On:
Sunday, April 22, 2001 10:48 PM
Hi,
If i understand correctly u wanna, sort elements in the hastable (for eg:-if u have state combo box, u wanna sort it alphabetically).
If ur using a hashtable the only solution which i suggest is something like this...
Hashtable has a method ketSet, which returns a Set interface. Set interface has a method toArray which returns a Object array. Pass this object array to a method in Arrays.sort(Object o[]). Ur keys get sorted. Now get the values from the hashtable which gets sorted automatically.....
This is a round about method....-:)
Re: How can I sort a ENTIRE PAIR key/value, sort by the value, in a Hashtable?
Posted By:
AlessandroA_Garbagnati
Posted On:
Wednesday, April 4, 2001 10:16 AM
Nestor,
I think that the best and cleanes way to achieve that is using a java.util.TreeMap instead of a java.util.Hashtable (they both implements java.util.Map).
Then, you create your own java.util.Comparator that sorts the elements based on your specific needs. You create a TreeMap using that Comparator (it's one of the contructor) and that's it. Every time you'll insert a new key/pair, it will be sorted in the map.