Kotlin Program to sort an array in descending order


In this program, You will learn how to sort an array in descending order in Kotlin.


Array list: 6 4 1 2 3 5

List in descending order: 6 5 4 3 2 1

Example: How to sort an array in descending order in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    val arr = IntArray(6)
    var temp: Int

    val sc = Scanner(System.`in`)

    print("Enter 6 elements:")

    for (i in 0 until arr.size) {
        arr[i] = sc.nextInt()
    }

    for (i in 0 until arr.size) {
        for (j in i + 1 until arr.size) {
            if (arr[i] < arr[j]) {
                temp = arr[i]
                arr[i] = arr[j]
                arr[j] = temp
            }
        }
    }

    print("Array list after sorting:")

    for (n in arr) {
        print("$n ")
    }
}

Output:

Enter 6 elements:30 40 10 20 50 60
Array list after sorting:60 50 40 30 20 10