I have the following Key : Value pairs.
A56:A64=9, A65:A73=9, A2:A8=7, A49:A55=7, A20:A26=7, A9:A19=11, A43:A48=6, A27:A42=16
I want to sort them in an ascending order. I tried using a TreeMap
but a got this :
{A20:A26=7, A27:A42=16, A2:A8=7, A43:A48=6, A49:A55=7, A56:A64=9, A65:A73=9, A9:A19=11}
A2:A8=7 should be first, but it is coming third.
Please let me know how I can fix this.
TreeMap
for a String
key would use String lexicographical order by default (that's the natural ordering for String
s), unless you supply your own Comparator in the constructor.
A2:A8 comes after A20:A26 when using lexicographical order.
Your comparator would probably have to split the String key into 4 parts (for example, A20:A26
would be split to A
, 20
, A
and 26
) and compare each pair of parts separately, using integer comparison for the integer parts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With