5

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.

Eran
  • 374,785
  • 51
  • 663
  • 734
Umesh Kumar
  • 1,388
  • 2
  • 13
  • 31

1 Answers1

7

TreeMap for a String key would use String lexicographical order by default (that's the natural ordering for Strings), 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.

Eran
  • 374,785
  • 51
  • 663
  • 734