Archive for November, 2008

Java: Map sorted by value

Today, I wanted to solve a – at first sight – rather simple problem in Java. I wanted to iterate over the keys and values of a Map in an order given by its values. E.g.

Map m = new ...
m.put("foo", 47);
m.put("bar", 11);
m.put("baz", 11);
m.put("qux", 100);
// {bar=11, baz=11, foo=47, qux=100}
System.out.println(m);
 
m.put("foo", 101);
// {bar=11, baz=11, qux=100, foo=101}
System.out.println(m);

Read the rest of this entry »