-2

I want following keys to be sorted in the given following order. D11, D21, D31, D101, D201, D301, D401, Q11, Q40, Q102, Q401.

Please help.

lesnar
  • 2,400
  • 7
  • 41
  • 72
user3169322
  • 9
  • 1
  • 4

1 Answers1

0

You will need to implement your own Comparator for your TreeMap, which you will then pass into its constructor.

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • public void sortedMap(TreeMap srt) {Set> en = srt.entrySet(); List> list = new ArrayList>(en); Collections.sort(list, new Comparator>(){ String [] ob1, ob2 = null; Integer i1, i2; public int compare(Entry o1, Entry o2) { ob1 = o1.getKey().split("(?<=\\D)(?=\\d)"); ob2 = o2.getKey().split("(?<=\\D)(?=\\d)"); i1 = Integer.parseInt(ob1[1]); i2 = Integer.parseInt(ob2[1]); return o1.compareTo(o2); } }); } – user3169322 Nov 26 '16 at 14:18
  • I have written above function but it's still not working in my case – user3169322 Nov 26 '16 at 14:18
  • 1
    Please place this into the question and format it so that I can understand it. And while you're at it, please elaborate on "it's still not working" means, preferably with either an error message or a comparison between expected and actual output. – Joe C Nov 26 '16 at 16:09