How to sort a list of objects in case of an insensitive order?

Let's say I have a list of lines in Kotlin: stringList: MutableList<String>

Then it is easy to sort such a list in case of an insensitive order by doing this:

stringList.sortWith(String.CASE_INSENSITIVE_ORDER)

But how would I sort the list of objects in case of an insensitive order? For example:places: MutableList<Place>

Where Placeis a simple class with 2 fields - name: Stringand id: Int, and I would like to sort these places in the field name.

I tried to do something like this: places.sortedWith(compareBy { it.name })but this solution is not case sensitive.

+6
source share
1 answer

, compareBy Comparator , . : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.comparisons/compare-by.html

Try:

places.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER, { it.name }))
+9

All Articles