My code as below and the output for treeMapMem as below :-
key value
1 a
10 b
2 c
3 d
for(Map.Entry<String,String> entry : treeMapMem.entrySet()) {
String value = entry.getValue();
myMenulistSeqMem.add(value);
}
My problem like this the sorting sequence is fine from 1,2,3 but 10 appear after 1.
That's because the default String Comparator
uses lexicographical order -- i.e. character by character, as in a dictionary. Since "1" comes before "2", any String starting with "1" will precede any other starting with "2".
You could create your own custom comparator to implement Natural Sorting and supply it to the TreeMap
's constructor, but in this case it would be far simpler to just define the map as TreeMap<Integer, String>
.