0

I have an issue with comparing Strings that sometimes contain numbers.

Let's say I have two Strings; s1 = "Chapter 9" and s2 = "Chapter 10"

Any human would say that, if these are to be sorted, s1 would end up earlier than s2 and that also happens in Windows folders. However, in Java it just checks one char at a time until one is greater/lesser than the other, which leads to it putting s2 earlier than s1 due to it having a '1' where s1 has a '9'

Is there a way of managing this that does not involve a lot of code and/or processing?

Anju Maaka
  • 263
  • 1
  • 10
  • That kind of depends on what you mean by "a lot of code and/or processing". Does getting the number using a regular expression count as "a lot of processing"? – Sweeper Feb 13 '20 at 10:18
  • 1
    You can implement your own Comparator, to sort these things like you wish. – Jan Held Feb 13 '20 at 10:18
  • 1
    sort would work if you can make *"Chapter 9"* into *"Chapter 09"* (unless you have chapter 100 and then you need to make it into *"Chapter 009"* :) – Sharon Ben Asher Feb 13 '20 at 10:19

0 Answers0