https://kotlinlang.org logo
k

Klaus

07/17/2020, 10:25 AM
Just converting the Java code to Kotlin seems a bit lame to me. 🙂
j

jbnizet

07/17/2020, 11:04 AM
What’s lame about it? Being compatible with Java is one of the greatest advantages of Kotlin:
Copy code
println(listOf("Zuhause", "Öl", "Auto", "Bank")
                .sortedWith(Collator.getInstance(Locale.GERMAN)))
👍 3
☝🏻 1
k

Klaus

07/17/2020, 12:51 PM
On iOS I always get blamed for not beeing swifty enough. So, I just asumed. 🙂
Thanks for your reply, works like a charme.
I have an array with objects, so I used this construction:
Copy code
val collator = Collator.getInstance(Locale("de", "DE"))
val sorted = filtered.sortedWith(compareBy(collator) { it.title })
so cool!
n

nkiesel

07/17/2020, 7:53 PM
filtered.map { collator.getCollationKey(it) }.sorted().map { it.sourceString }
sorts correctly and - according to Collator documentation - should be more efficient
fun Iterable<String>.sortedWith(collator: Collator) = this.map { collator.getCollationKey(it) }.sorted().map { it.sourceString }
allows to use
filtered.sortedWith(collator)