To sort an array in descending order in Kotlin, you can use the `sortedDescending()` function for both arrays of integers and arrays of strings.
In this example,
numbers
.sortedDescending()
function to sort the array of integers in descending order.fun main() {
val numbers = arrayOf(5, 2, 8, 1, 9)
val sortedNumbers = numbers.sortedDescending()
println(sortedNumbers.joinToString())
}
9, 8, 5, 2, 1
In this example,
words
.sortedDescending()
function to sort the array of strings in descending order.fun main() {
val words = arrayOf("apple", "banana", "cherry", "date")
val sortedWords = words.sortedDescending()
println(sortedWords.joinToString())
}
date, cherry, banana, apple
In this tutorial, we learned How to Sort an Array in Descending Order in Kotlin language with well detailed examples.