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>
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);
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