The sorted() Function is used to sort a Set in Ascending order and in addition it doesn't change the contents of the actual Set.
fun main() { var x = mutableSetOf("Mohan", "Kriti", "Salim") var y = x.sorted() println("The contents of the Set x is unchanged : "+x) println("The sorted Set assigned to Set y : "+y) }
So, in the above code we have created a Set and initialised to the variable x.
val x = mutableSetOf("Mohan", "Kriti", "Salim")
Below is how the values are positioned in the Set,
Then we have used the sorted() method to sort the Set x in ascending order.
var y = x.sorted()
And initialised the sorted Set to a new Set y.
Now, if you look at the output of y. You can see that the Set 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 set x is unchanged.