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




KOTLIN - SORT LIST ELEMENTS


How to sort a List in Ascending Order?


The 'sort()' Function is used to sort a List in Ascending order.


Example :



fun main() {
    val x = mutableListOf("Mohan", "Kriti", "Salim")
    x.sort()
    println("The Sorted List in ascending order is "+x)
}


Output :



 The Sorted List in ascending order is [Kriti, Mohan, Salim]

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


val x = mutableListOf("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the List,


java_Collections

Then we have used the 'sort()' method to sort the List 'x' in ascending order.


x.sort()

And the List 'x' gets sorted with 'Kriti' as the first value, 'Mohan' second and 'Salim' as the third.


java_Collections

And we get the below output.


The Sorted List in ascending order is [Kriti, Mohan, Salim]

Next let us see, how to sort a List in Descending order.


How to sort a List in Descending Order?


The 'sortDescending()' Function is used to sort a List in Descending order.


Example :



fun main() {
    val x = mutableListOf("Mohan", "Kriti", "Salim")
    x.sortDescending()
    println("The Sorted List in descending order is "+x)
}


Output :



 The Sorted List in descending order is [Salim, Mohan, Kriti]

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


val x = mutableListOf("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the List,


java_Collections

Then we have used the parameter 'reverse = True' along with the 'sort()' method to sort the List 'x' in descending order.


x.sortDescending()

And the List 'x' gets sorted in descending order with 'Salim' as the first value, 'Mohan' second and 'Kriti' as the third.


java_Collections

And we get the below output.


The Sorted List in descending order is [Salim, Mohan, Kriti]

How to sort a List with numbers in Increasing/Deceasing Order?


Even here the 'sort()' and 'sortDescending()' Function is used to sort a List with numbers in Increasing/Deceasing Order.


At first let us see an example to sort a List with numbers in Increasing order.


Example :



fun main() {
    val x = mutableListOf(5, 3, 2, 4)
    x.sort()
    println("The Sorted List in increasing order is "+x)
}


Output :



 The Sorted List in increasing order is [2, 3, 4, 5]

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


val x = mutableListOf(5, 3, 2, 4)

Below is how the values are positioned in the List,


java_Collections

Then we have used the 'sort()' method to sort the List 'x' in increasing order.


x.sort()

And the numbers in the List 'x' gets sorted.


java_Collections

And we get the below output.


The Sorted List in increasing order is [2, 3, 4, 5]

Next let us see, how to sort a List in Descending order.


Example :



fun main() {
    val x = mutableListOf(5, 3, 2, 4)
    x.sortDescending()
    println("The Sorted List in decreasing order is "+x)
}


Output :



 The Sorted List in decreasing order is [5, 4, 3, 2]

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


val x = mutableListOf(5, 3, 2, 4)

Below is how the values are positioned in the List,


java_Collections

Then we have used the parameter 'reverse = True' along with the 'sort()' method to sort the List 'x' in decreasing order.


x.sortDescending()

And the List 'x' gets sorted in decreasing order.


java_Collections

And we get the below output.


The Sorted List in decreasing order is [5, 4, 3, 2]

So, in the above examples we have seen that the sorting is done in the List itself.


i.e. We have created the List 'x' with values "Mohan", "Kriti" and "Salim".


And after sorting,


x.sort()

The elements of the List 'x' gets sorted.


java_Collections

Now, what if we don't want the values of List 'x' to be changed permanently?


Well! There is something called as 'sorted()' and 'sortedDescending()' method.


Let's understand with the below example.


Sorting a List in Ascending Order using sorted() method


The 'sorted()' Function is used to sort a List in Ascending order and in addition it doesn't change the contents of the actual List.


Example :



fun main() {
    var x = mutableListOf("Mohan", "Kriti", "Salim")
    var y = x.sorted()
    println("The contents of the List x is unchanged : "+x)
    println("The sorted List assigned to List y : "+y)
}


Output :



 The contents of the List x is unchanged : [Mohan, Kriti, Salim]
 The sorted List assigned to List y : [Kriti, Mohan, Salim]

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


val x = mutableListOf("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the List,


java_Collections

Then we have used the 'sorted()' method to sort the List 'x' in ascending order.


var y = x.sorted()

And initialised the sorted List to a new List 'y'.


Now, if you look at the output of 'y'. You can see that the List 'y' has the sorted values with 'Kriti' as the first value, 'Mohan' second and 'Salim' as the third.


java_Collections

And when you look at the value of 'x',


java_Collections

The values of the list 'x' is unchanged.


Similarly, there is 'sortedDescending()' method to sort the elements of List in Descending order.