Home > Mobile >  Sorting special character strings in Kotlin
Sorting special character strings in Kotlin

Time:03-17

I have got a list of strings that contain special characters.

For example:

["Geben", "Zurück", "Über", "Äpfel"]

I need to sort this list in Kotlin to have the following output:

Ascending: ["Äpfel", "Geben", "Über", "Zurück"]

Descending: ["Zurück", "Über", "Geben", "Äpfel"]

How to do this?

Edit: Using sortedBy gives the following output which is not desired:

["Geben", "Zurück", "Äpfel", "Über"]

CodePudding user response:

You can achieve this by using a collator that recognizes German umlauts:

fun Iterable<String>.sortedWithUmlauts() =
    sortedWith(Collator.getInstance(Locale("de", "DE")))
  • Related