sort hashmap by value using java 1.4
807607Nov 21 2006 — edited Nov 21 2006I have to following HashMap
Map students = new HashMap();
students.put("A", new Integer(12));
students.put("B", new Integer(60));
students.put("C", new Integer(80));
students.put("D", new Integer(71));
students.put("E", new Integer(30));
students.put("F", new Integer(45));
By making a TreeMap I can sort the Map on the basis of keys, but how should I sort the Map on the basis of values.
The key value pairing should remain intact.
If I use the sort method and then put the values back, I'll get
A, 12
B, 30
C, 45
D, 60
E, 71
F, 80
In this case the key value combination is distorted.
What I need is
A, 12
E, 30
F, 45
B, 60
D, 71
C, 80
Say this example is students and their marks Map. A scored 12, B scored 60..
I want to sort the students on the basis of their marks.