If you add the following conversion function (in scala), you can alphabetically and numerically sort all the lines:
def transformed(s: String): String = { s.replaceAll("""(?<=[^\d]|^)(\d)(?=[^\d]|$)""","""000$1""") .replaceAll("""(?<=[^\d]|^)(\d\d)(?=[^\d]|$)""","""00$1""") .replaceAll("""(?<=[^\d]|^)(\d\d\d)(?=[^\d]|$)""","""0$1""") }
Basically, it replaces each numeric number with a fixed integer width, so the alphabetical sort in this case is equal to the numeric sort.
Testing at your input:
> val s = List("10-PDW", "100-PDW", "110-PDW", "2-PDW") > s.sortBy(transformed) res2: List[String] = List(2-PDW, 10-PDW, 100-PDW, 110-PDW)
This only works if you are sure that all numbers are below 9999. If you have more numbers, you should consider expanding the function or doing something else.
source share