Sorting hashtable entries
i need to sort hashtable entries according to the values (objects).
eg. i have hash table as follows:
keyvalue
cat2
dog5
cow5
mouse1
value represents a class containing 'int'. i need to sort according to this integer value and get a hashtable as follows
keyvalue
dog5
cow5
cat2
mouse1
or just get keys as an array (or vector) as
dog
cow
cat
mouse
thanks
[471 byte] By [
tomcla] at [2007-9-27 11:55:13]

TreeMap will not sort values. So you can either populate it with the keys and values reversed, or if you only care about getting a sorted list of the integers, you could use a TreeSet.
> TreeMap will not sort values. So you can either
> populate it with the keys and values reversed
This won't work if the values are not unique.
If you just need a sorted list of the values you can do this:
ArrayList values = new ArrayList(new hashtable.values()); //use HashMap instead
Collections.sort(values);
The problem is, if you need the keys in this order you will have to relate them back to the keys, which can be slow. Is that what you need to do?