How do i sort list in kotlin - Sort Mutablelist

Last updated Oct 07, 2021


In this Kotlin programming example we will learn how to sort a list in kotlin. To sort a list we will use following ways

  • sorted()
  • sortedDescending()
  • sort()

 

Using sorted() method:

sorted() is a collection class method, which will sort the given list and return new list. This will sort the list in ascending order

Syntax:

public fun > Iterable.sorted(): List

 

Write program to sort the list using sorted() method

import java.util.*

fun main() {
    var k:Int;
    var inputArray: MutableList<Int> = mutableListOf();
    val integer = Scanner(System.`in`)
    for( k in 0..4)
    {

        print("Enter an integer at position $k : ")
        var enteredinteger:Int = integer.nextInt()
        inputArray.add(enteredinteger)
    }
    println("Given list : ${(inputArray)}")

   var sortedlist= inputArray.sorted()

    println("After sort the list : ${(sortedlist)}")


}

 

Output:

Enter an integer at position 0 : 12
Enter an integer at position 1 : 3
Enter an integer at position 2 : 35
Enter an integer at position 3 : 2
Enter an integer at position 4 : 135
Given list : [12, 3, 35, 2, 135]
After sort the list : [2, 3, 12, 35, 135]

 

Sort in descending order

To sort the list in descending order collection class has other method called sortedDescending(). This method will sort the list in descending order and returns new list with sorted items.

Syntax:

public fun > Iterable.sortedDescending(): List

 

Write program to sort list in descending order

import java.util.*

fun main() {
    var k:Int;
    var inputArray: MutableList<Int> = mutableListOf();
    val integer = Scanner(System.`in`)
    for( k in 0..4)
    {

        print("Enter an integer at position $k : ")
        var enteredinteger:Int = integer.nextInt()
        inputArray.add(enteredinteger)
    }
    println("Given list : ${(inputArray)}")

   var sortedlist= inputArray.sortedDescending()

    println("After sort the list : ${(sortedlist)}")


}

 

Output:

Enter an integer at position 0 : 12
Enter an integer at position 1 : 1
Enter an integer at position 2 : 23
Enter an integer at position 3 : 124
Enter an integer at position 4 : 21
Given list : [12, 1, 23, 124, 21]
After sort the list : [124, 23, 21, 12, 1]

 

Using sort()

unlike sorted, sortedDescending() we have other method sort() which will sort the list items and it will not create new list, it will update on the same list.

 

import java.util.*

fun main() {
    var k:Int;
    var inputArray: MutableList<Int> = mutableListOf();
    val integer = Scanner(System.`in`)
    for( k in 0..4)
    {

        print("Enter an integer at position $k : ")
        var enteredinteger:Int = integer.nextInt()
        inputArray.add(enteredinteger)
    }
    println("Given list : ${(inputArray)}")

   inputArray.sort()

    println("After sort the list : ${(inputArray)}")


}

 

output:

Enter an integer at position 0 : 1
Enter an integer at position 1 : 23
Enter an integer at position 2 : 2
Enter an integer at position 3 : 145
Enter an integer at position 4 : 35
Given list : [1, 23, 2, 145, 35]
After sort the list : [1, 2, 23, 35, 145]

 

Conclusion: In this example we covered how to sort mutable list using sort,sorted,and sortdescending methods. We have other sort methods which will use to sort list of custom objects. we will cover this in next section.

 


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

12 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions