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




PYTHON - SORT SET ELEMENTS


How to sort a Set in Ascending/Descending Order?


The 'sorted( )' Function is used to sort a Set in Ascending/Descending order.


At first let us see an example to sort a Set in Ascending order.


Example :


x = {"Mohan", "Kriti", "Salim"} 
sorted(x)
print("The Sorted Set in ascending order is ",x) 


Output :



  The Sorted Set in ascending order is {'Kriti', 'Mohan', 'Salim'}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = {"Mohan", "Kriti", "Salim"}

java_Collections

Then we have used the 'sorted( )' Function to sort the Set 'x' in ascending order.


sorted(x)

And the Set 'x' gets sorted with 'Kriti' as the first value, 'Mohan' second and 'Salim' as the third.


java_Collections

And we get the below output.


The Sorted Set in ascending order is {'Kriti', 'Mohan', 'Salim'}

Next let us see, how to sort a Set in Descending order.


Example :


x = {"Mohan", "Kriti", "Salim"} 
sorted(x,reverse = True)
print("The Sorted Set in descending order is ",x) 


Output :



  The Sorted Set in descending order is {'Salim', 'Kriti', 'Mohan'}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = {"Mohan", "Kriti", "Salim"}

java_Collections

Then we have used the parameter 'reverse = True' and name of the set 'x' along with the 'sorted( )' method to sort the Set 'x' in descending order.


sorted(x,reverse = True)

And the Set '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 Set in descending order is {'Salim', 'Kriti', 'Mohan'}

How to sort a Set with numbers in Increasing/Deceasing Order?


Even here the 'sorted( )' Function is used to sort a Set with numbers in Increasing/Deceasing Order.


At first let us see an example to sort a Set with numbers in Increasing order.


Example :


x = {5, 3, 2, 4} 
sorted(x)
print("The Sorted Set in increasing order is ",x) 


Output :



  The Sorted Set in increasing order is {2, 3, 4, 5}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = {5, 3, 2, 4}

java_Collections

Then we have used the 'sorted( )' method to sort the Set 'x' in increasing order.


sorted(x)

And the numbers in the Set 'x' gets sorted.


java_Collections

And we get the below output.


The Sorted Set in increasing order is {2, 3, 4, 5}

Next let us see, how to sort a Set in Descending order.


Example :


x = {5, 3, 2, 4}
sorted(x, reverse = True)
print("The Sorted Set in decreasing order is ",x) 


Output :



  The Sorted Set in decreasing order is {5, 4, 3, 2}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = {5, 3, 2, 4}

java_Collections

Then we have used the parameter 'reverse = True' along with the 'sorted( )' method to sort the Set 'x' in decreasing order.


sorted(x, reverse = True)

And the Set 'x' gets sorted in decreasing order.


java_Collections

And we get the below output.


The Sorted Set in decreasing order is {5, 4, 3, 2}