1

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"]

2 Answers 2

6

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

fun Iterable<String>.sortedWithUmlauts() =
    sortedWith(Collator.getInstance(Locale("de", "DE")))
Sign up to request clarification or add additional context in comments.

1 Comment

Is there any way, this can be done generically, for instance if the list contains strings of German as well as French language?
1

As every language has its own sorting rules there is no sorting for two languages "out of the box". But as experimented myself even the German collator instance gave good result for a list of mixed French German words. Moreover you can set the strength property of the collator control the sort algorithm - dependent on which results you expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.