Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - SORT ARRAY ELEMENTS


How to sort a String type Array in Ascending Order?


The 'sort()' method is used to sort a String Array in Ascending order.


Example :



fun main() {

    var arr = arrayOf("Mohan", "John", "Paul", "Kriti", "Salim")
    arr.sort()
        
    println("The Sorted Array in ascending order is : ")
        
    for (str in arr) {
        println(str)
    }
}


Output :


The Sorted Array in ascending order is :



 John
 Kriti
 Mohan
 Paul
 Salim

So, in the above code we have created a 'Array' and initialised to the variable 'arr'.


var arr = arrayOf("Mohan", "John", "Paul", "Kriti", "Salim")

Below is how the values are positioned in the Array,


java_Collections

Then we have used the 'sort()' Function to sort the Array 'arr' in ascending order.


arr.sort()

And the Array 'arr' gets sorted with 'John' as the first value, 'Kriti' second and 'Mohan' as the third, 'Paul' as fourth and 'Salim' as the fifth value.


java_Collections

And we get the below output.



Output :


The Sorted Array in ascending order is :



 John
 Kriti
 Mohan
 Paul
 Salim

How to sort a Array with numbers in Increasing Order?


Even here the 'sort()' method is used to sort the numbers in Increasing Order.


Example :



fun main() {

    var arr = arrayOf(5, 3, 2, 4)
    arr.sort()
        
    println("The Sorted Array in ascending order is : ")
        
    for (str in arr) {
        println(str)
    }
}


Output :


The Sorted Array in ascending order is :



 2
 3
 4
 5

So, in the above code we have created an Integer 'Array' and initialised to the variable 'arr'.


var arr = arrayOf(5, 3, 2, 4)

Below is how the values are positioned in the Array,


java_Collections

Then we have used the 'Ints()' Function from the 'sort' package to sort the Array 'arr' in increasing order.


arr.sort()

And the numbers in the Array 'arr' gets sorted.


java_Collections

And we get the below output.



Output :


The Sorted Array in ascending order is :



 2
 3
 4
 5

How to sort a Array with numbers in Decreasing Order?


Even here the 'sortDescending()' method is used to sort the numbers in Decreasing Order.


Example :



fun main() {

    var arr = arrayOf(5, 3, 2, 4)
    arr.sortDescending()
        
    println("The Sorted Array in decreasing order is : ")
        
    for (str in arr) {
        println(str)
    }
}


Output :


The Sorted Array in decreasing order is :



 5
 4
 3
 2

How to sort a Array with numbers in Increasing Order specifying the range?


Even here the 'sort()' method is used to sort the numbers in Increasing Order by specifying the range.


Example :



fun main() {

    var arr = arrayOf(5, 3, 2, 4)
    arr.sort(0,2)
        
    println("The Sorted Array in increasing order is : ")
        
    for (str in arr) {
        println(str)
    }
}


Output :


The Sorted Array in increasing order is :



 3
 5
 2
 4

So, we have the below array,


var arr = arrayOf(5, 3, 2, 4)

java_Collections

Now, only the first two values of the array is sorted. And the last two values are left as is.


arr.sort(0,2)

This is because we have specified the range. i.e. From 0th index to 2nd position(i.e. '1' index).