Kotlin example - how to sort an array Ascending and descending order

Published April 21, 2021


In this kotlin programming example we will learn how to sort an array in ascending and descending order. In this example we will create an array by taking the array elements from user inputs using Scanner class.

 

Example to Sort array in Ascending order

package com.rrtutors.lib


import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {

    val s = Scanner(System.`in`)

    //Input Array Size
    print("Enter array size: ")
    val size = s.nextInt()

    //Create Integer array of Given size
    val intArray = IntArray(size)

    //Input array elements
    println("Enter Array Elements: ")
    for (i in intArray.indices) {
        print("intArray[$i] : ")
        intArray[i] = s.nextInt()
    }

    //to Perform Ascending Order Sorting on integer Array
    var temp:Int
    for (i in intArray.indices) {
        for (j in i + 1 until intArray.size) {
            if (intArray[i] > intArray[j]) {
                temp = intArray[i]
                intArray[i] = intArray[j]
                intArray[j] = temp
            }
        }
    }

    print("Ascending Order: ")
    for (item in intArray) {
        print("$item ")
    }
}

 

 

Output:

Enter number of elements in the array: 5
Enter Arrays Elements:
intArray[0] : 12
intArray[1] : 2
intArray[2] : 234
intArray[3] : 4
intArray[4] : 1
Ascending Order: 1 2 4 12 234
Process finished with exit code 0

 

 

Example to sort Array in Descending order

package com.rrtutors.lib


import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {

    val s = Scanner(System.`in`)

    //Input Array Size
    print("Enter array size: ")
    val size = s.nextInt()

    //Create Integer array of Given size
    val intArray = IntArray(size)

    //Input array elements
    println("Enter Array Elements: ")
    for (i in intArray.indices) {
        print("intArray[$i] : ")
        intArray[i] = s.nextInt()
    }

    //to Perform Descending Order Sorting on integer Array
    var temp:Int
    for (i in intArray.indices) {
        for (j in i + 1 until intArray.size) {
            if (intArray[i] < intArray[j]) {
                temp = intArray[i]
                intArray[i] = intArray[j]
                intArray[j] = temp
            }
        }
    }

    print("Descending Order: ")
    for (item in intArray) {
        print("$item ")
    }
}

 

Output:

Enter array size: 5
Enter Array Elements:
intArray[0] : 12
intArray[1] : 2
intArray[2] : 234
intArray[3] : 4
intArray[4] : 1
Descending Order: 234 12 4 2 1
Process finished with exit code 0

 

There is an inbuilt methods to sort an array in kotlin

  1. Sort()
  2. sortDescending()

 

By using these inbuilt methods we can sort array in kotlin

 

 

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

205 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions