Ok, here is some code that essentially reverses the TreeMap, i.e. it swaps the values with the keys so that the new Treemap is sorted by the values of the previous tree. Of course, this means that the keys become the values and vice versa but this shouldn't matter to you for output purposes. You can then use the iterator to populate your HTML code dynamically via a servlet of jsp or whatever.
Since a TreeMap is guaranteed to have log(N) lookup and insertion, this is relatively inexpensive, i.e. O(nlogn)
public static TreeMap keysToValues(TreeMap t)
{
// Create map to be returned
TreeMap newmap = new TreeMap();
// Get the keys from the incoming TreeMap
Set keys = t.keySet();
Iterator i = keys.iterator();
String key = null;
//Extract key/value pairs and insert in new Tree
while (i.hasNext())
{
key = (String)i.next();
newmap.put(t.get(key), key);
}
return newmap;
}
Hope this helps...