0

For example i have List with content: {1 5 2b 10 2a 3 7}, and i need sort it in alphabet order as: {1 2a 2b 3 5 7 10}. I tried with Comparator interface as:

Collections.sort(list);

But it returned array {1 10 2a 2b 3 5 7}. How i can do it in Java?

Werder
  • 383
  • 2
  • 4
  • 16
  • 1
    Are there any strings where there is no number in front? – laune Mar 31 '15 at 14:22
  • 3
    Is [this](http://stackoverflow.com/a/1262402/335858) what you are looking for? – Sergey Kalinichenko Mar 31 '15 at 14:23
  • @laune, no, all strings have number in front – Werder Mar 31 '15 at 14:25
  • You tried implementing the Comparator interface? Show us the implementation then. The mistake must be in there. – Manu Mar 31 '15 at 14:27
  • See http://stackoverflow.com/questions/3552756/best-way-to-get-integer-part-of-the-string-600sp for how to get the int part, then sort by that. – Adrian Leonhard Mar 31 '15 at 14:27
  • @Adrian Leonhard Yes, i tried custom Comparator with override method compareTo, but i need compare string to, for example {2b, 2c, 2a} -> {2a, 2b, 2c}, so i can't use number without letter after – Werder Mar 31 '15 at 14:33
  • @dasblinkenlight, Thank you ver much for link! NaturalOrderComparator.java is really work ) – Werder Mar 31 '15 at 14:43

0 Answers0