help please! sorting hashmap

hi everyonei have a hash map that stores strings with a corresponding value. someMap.put(wordString, numericValue);How would I sort that in decending order of numbericValue and print it in a JList?Many thanks
[243 byte] By [sarahsmitha] at [2007-11-26 22:51:54]
# 1
Take the values out with Map.values(), stick them in a List, then use Collections.sort to sort it, using a Comparator. All these classes/interfaces are in java.util.
paulcwa at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 2

hi thanks for your advice. here is what ive compiled from forum sources.

HashMap map = new LinkedHashMap();

List mapKeys = new ArrayList(hmap.keySet());

List mapValues = new ArrayList(hmap.values());

hmap.clear();

TreeSet sortedSet = new TreeSet(mapValues);

Object[] sortedArray = sortedSet.toArray();

int size = sortedArray.length;

// a) Ascending sort

for (int i=0; i<size; i++) {

map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])),

sortedArray[i]);

}

i dont think that is the right way to go about returning to a JList

Regards>

sarahsmitha at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 3

Well, you have cobbled together a mess of code there. And you're right, it doesn't put anything into a JList. But that wouldn't have been hard if you had looked in the documentation and found the constructor new JList(Object[]). Let's see if we can extract the useful bits from that code:List mapValues = new ArrayList(hmap.values());

Collections.sort(mapValues); // use a Comparator if you need to

Object[] sortedArray = mapValues.toArray();

JList guiList = new JList(sortedArray);

DrClapa at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 4

Are you trying to make your code as complicated and convoluted as possible? [edit: was referring to OP's code here]

Suppose you have a map "myMap":

Vector data = new ArrayList(myMap.values());

Collections.sort(data, new Comparator() {

public int compare(Object o1, Object o2) {

return ((Comparable)o2).compareTo(o1);

}

});

JList = new JList(data);

I'm pretty certain that'll do it.

Message was edited by:

paulcw

paulcwa at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 5
what's the OP?not trying to make code complicated- havent used JLists or hash maps before. anyway your code comes up with an error. says require java.util.Vector found java.util.ArrayListthanks
sarahsmitha at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 6
OP = original poster ie you.
floundera at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 7
> says require java.util.Vector found java.util.ArrayListOK so change that.
ejpa at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...
# 8
My mistake. But easy to fix.I'm kind of surprised that JList supports Vectors but not generic lists.Message was edited by: paulcw
paulcwa at 2007-7-10 12:14:15 > top of Java-index,Java Essentials,Java Programming...