I am using function which returns key value pair in LinkedHashMap.
LinkedHashMap<Integer,String> lhm = new LinkedHashMap<Integer,String>();
// Put elements to the map
lhm.put(10001, "Stack");
lhm.put(10002, "Heap");
lhm.put(10003, "Args");
lhm.put(10004, "Manus");
lhm.put(10005, "Zorat");
Note: I cannot change LinkedHashMap to any other map in my code as the function is being used in several other functions.
I have also googled and tried to use TreeMap which gives us the desired result in ascending order. But, here in TreeMap key is in ascending order and not the values.
My requirement is mainly for values.
How can I get values in Ascending order.
Desired Output
10003, "Args"
10002, "Heap"
10004, "Manus"
10001, "Stack"
10005, "Zorat"
Thank you in advance !!!!
You need a comparator for this
Comparator<Entry<String, String>> valueComparator = new
Comparator<Entry<String,String>>() {
@Override public int compare(Entry<String, String> e1, Entry<String,
String> e2) {
String v1 = e1.getValue(); String v2 = e2.getValue(); return
v1.compareTo(v2);
}
};