This article explores different ways to sort a mutable list in reverse order in Kotlin. The sorting would be done in-place and is stable.

1. Using sortDescending() function

To sort the list in reverse order, you can use the native sortDescending() function.

Download Code

2. Using sortByDescending() function

The sortByDescending() function sorts elements of the list descending according to the sort order of the value returned by the specified selector function.

Download Code

3. Using sortWith() function

You can also sort the list according to the order specified by a comparator using the sortWith() function.

Download Code

 
You can also implement your own reverse comparator:

Download Code

4. Using sort() function

Another plausible way to get the reversed list is to sort it first in ascending order and then reverse the sorted list.

Download Code

5. Using sortedWith() function

Finally, if you have an immutable list instance, you can use the sortedWith() function to create a new list of elements sorted in reverse order using order induced by the specified comparator.

Download Code

That’s all about sorting a list in reverse order in Kotlin.