You can also reverse the map while sorting it by value if you want. Be aware of the implication of the uniqueness of keys though:
Map map = new TreeMap();
map.put("1", "1000");
map.put("6", "4000");
map.put("8", "3000");
map.put("25", "10000");
map.put("13", "5000");
map.put("19", "6000");
Map reverseMap = new TreeMap(new ValSorter());
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry e = (Map.Entry)iter.next();
reverseMap.put(e.getValue(
}
static class ValSorter implements Comparator {
public int compare(Object o1, Object o2) {
return new Integer((String) o1).compareTo(new Integer((String) o2));
}
}
Main Topics
Browse All Topics





by: aozarovPosted on 2005-05-11 at 18:41:42ID: 13983366
ArrayList list = new ArrayList(map.values()); f_values_a re_not_com parable>);
Collections.sort(list); // or Collections.sort(list, <provide_your_comparator_i