To sort a list of strings in Kotlin in dictionary order, you can use the `sorted` or `sortedBy` function.
In this example,
words
with some strings.sorted
function to sort the list in dictionary order.fun main() {
val words = listOf("banana", "apple", "grape", "orange")
val sortedWords = words.sorted()
println("Sorted list in dictionary order: $sortedWords")
}
Sorted list in dictionary order: [apple, banana, grape, orange]
In this example,
words
with some strings.sortedBy
function with a lambda function that returns the string itself to sort the list in dictionary order.fun main() {
val words = listOf("banana", "apple", "grape", "orange")
val sortedWords = words.sortedBy { it }
println("Sorted list in dictionary order: $sortedWords")
}
Sorted list in dictionary order: [apple, banana, grape, orange]
In this tutorial, we learned How to Sort a List of Strings in Dictionary Order in Kotlin language with well detailed examples.