In this Kotlin example, we will discuss how to sort Kotlin arrays with an example.
Sorting arrays in Kotlin example
All Kotlin examples at All Kotlin Examples.
The example sorts an array is ascending order with sortedArray() and descending order with sortedArrayDescending(). The methods create new sorted arrays.
fun main() {
val nums = arrayOf(7, 3, 3, 4, 5, 9, 1)
val sortedNums = nums.sortedArray()
println(Arrays.toString(sortedNums))
val sortedNumsDesc = nums.sortedArrayDescending()
println(Arrays.toString(sortedNumsDesc))
}
Output:
[1, 3, 3, 4, 5, 7, 9]
[9, 7, 5, 4, 3, 3, 1]
All Kotlin examples at All Kotlin Examples.
Comments
Post a Comment