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




LIST - sortDescending()


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


How to sort a List of Strings 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]

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]