The sortedDescending() Function is used to sort a Set in Descending 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.sortedDescending()
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,
-Function1.png)
Then we have used the sortedDescending() method to sort the Set x in descending order.
var y = x.sortedDescending()
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.
-Function2.png)
And when you look at the value of x,
-Function3.png)
The values of the set x is unchanged.