The 'sort()' Function is used to sort a List in Ascending order.
fun main() { val x = mutableListOf("Mohan", "Kriti", "Salim") x.sort() println("The Sorted List in ascending order is "+x) }
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
Then we have used the 'sort()' method to sort the List 'x' in ascending order.
And the List 'x' gets sorted with 'Kriti' as the first value, 'Mohan' second and 'Salim' as the third.
And we get the below output.
Next let us see, how to sort a List in Descending order.
The 'sortDescending()' Function is used to sort a List in Descending order.
fun main() { val x = mutableListOf("Mohan", "Kriti", "Salim") x.sortDescending() println("The Sorted List in descending order is "+x) }
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
Then we have used the parameter 'reverse = True' along with the 'sort()' method to sort the List 'x' in descending order.
And the List 'x' gets sorted in descending order with 'Salim' as the first value, 'Mohan' second and 'Kriti' as the third.
And we get the below output.
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.
fun main() { val x = mutableListOf(5, 3, 2, 4) x.sort() println("The Sorted List in increasing order is "+x) }
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
Then we have used the 'sort()' method to sort the List 'x' in increasing order.
And the numbers in the List 'x' gets sorted.
And we get the below output.
Next let us see, how to sort a List in Descending order.
fun main() { val x = mutableListOf(5, 3, 2, 4) x.sortDescending() println("The Sorted List in decreasing order is "+x) }
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
Then we have used the parameter 'reverse = True' along with the 'sort()' method to sort the List 'x' in decreasing order.
And the List 'x' gets sorted in decreasing order.
And we get the below output.
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,
The elements of the List 'x' gets sorted.
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.
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.
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) }
So, in the above code we have created a 'List' and initialised to the variable 'x'.
Below is how the values are positioned in the List,
Then we have used the 'sorted()' method to sort the List 'x' in ascending order.
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.
And when you look at the value of 'x',
The values of the list 'x' is unchanged.
Similarly, there is 'sortedDescending()' method to sort the elements of List in Descending order.