Firstly, I’d like to write few words to explain the observed behaviour. It is correct and expected. String objects are compared character by character referring to an ordinal number of a character in the ASCII table (http://www.asciitable.com/). In case of two strings have identical start, the longer string is considered bigger. This is why "file name 10” is bigger than “file name 1” and “file name 2” is bigger than “file name 10”.
The behaviour you want to achieve requires to include one more factor to the comparison mechanism: to take numbers into account and compare them using arithmetical rules.
I suppose the case you posted as an example is partial and simple one: there is no number or only one number and it is always placed at the end of a string. Whereas a full problem may imply that a string may contain an arbitrary number of numbers located at any position.
I’ll give you a one possible solution to this problem, which solves the simple case (the one you described):
data class Item(val text: String)
data class SplitItem(val text: String, val number: Int?)
fun SplitItem.toItem() = Item(text + (number?.let { " $it" } ?: ""))
fun main() {
val items = listOf(
Item("file name with default language"),
Item("file name 1"),
Item("file name 10"),
Item("file name 11"),
Item("file name 2"),
Item("file name 20")
).shuffled()
val splitItems = items.map {
val split = it.text.split(' ')
try {
val number = Integer.parseInt(split.last())
SplitItem(
text = split.dropLast(1).joinToString(separator = " "),
number = number
)
} catch (e: NumberFormatException) {
SplitItem(
text = it.text,
number = null
)
}
}
val sortedItems = splitItems.sortedWith { o1, o2 ->
val n = compareValues(o1.number, o2.number)
val t = compareValues(o1.text, o2.text)
compareValues(n, t)
}
sortedItems
.map { it.toItem() }
.forEach { println(it) }
}
Which produces the next output:
Item(text=file name with default language)
Item(text=file name 1)
Item(text=file name 2)
Item(text=file name 10)
Item(text=file name 11)
Item(text=file name 20)
To solve the full problem I’d suggest you to evolve this approach.