Kotlin sorting and binary search
sorted() returns a new list in natural order, sortedDescending() the reverse, and sortedBy { key } sorts by a key function. These functions do not change the original list, and the sort is stable, so equal elements keep their order. For text, natural order compares the characters, so capital letters come before lowercase ones. Sorting by lowercase gives the order people expect.
Kotlin
fun main() {
val numbers = listOf(5, 2, 9, 1, 5, 6)
println(numbers.sorted())
println(numbers.sortedDescending())
val sorted = numbers.sorted()
println(sorted.binarySearch(9))
val missing = sorted.binarySearch(4)
println("4 would be inserted at ${-missing - 1}")
val words = listOf("pear", "apple", "Fig")
println(words.sorted())
println(words.sortedBy { it.lowercase() })
val people = listOf("Ada" to 30, "Bo" to 25, "Cy" to 30, "Di" to 25)
println(people.sortedBy { it.second }.map { it.first })
}