Your can learn more about Comparator through the
Java Tutorial, or most good basic Java texts. In Peter's code, the entrySet() method returns a Set of Map.Entry objects. Each of these objects cointains a key and a value; you can use getKey() and getValue() to access them. Peter is assuming you really do need to sort things by value (rather than key), so his Comparator uses getValue to access the values of two different Map.Entry objects and compare them.
I agree with Peter that the idea of ordering a HashMap is... odd. Hashtable and HashMap have no order really (none worth trying to understand anyway). If you want a Map which is sorted based on the order of the
keys, you could use a TreeMap. If you really want something sorted by
value, I suppose you could use a LinkedHashMap. After sorting the ArrayList containing all the entries, put them in a LinkedHashMap:
Now any time you iterate through the entrySet(), keySet(), or values() of this map, elements will be found in the order they were inserted. If you add any new elements to the map later, they will occur at the end - probably you would need to re-sort all the entries again and put them in a new LinkedHashMap. (Or remove them all from the old map and then re-insert them in the new order.)