Updated April 14, 2023
Introduction to Python Sorted Function
Sorting in Python is quite simplified with the usage of Python Sorted functions, which eventually returns a sorted list of the passed iterable objects either in ascending or descending order as specified by the developer. Numbers are sorted numerically, whereas Strings are alphabetical. Sorted() is a built-in function within Python’s library that does all the hard work for you once you pass the iterable objects like lists, tuples, etc., along with a specified order in which you want the iterable passed to be sorted.
Syntax of in-built sorted() is as mentioned below:
sorted (iterable, key, Sorting_order)
- Iterable: This is a required argument. An iterable is nothing but a list, dictionary, set, forzenset, or even a tuple that can be iterated element by element (Or any other iterator that can be sorted).
- Key: This is an optional argument. Key is primarily a function that serves as a key for sorting comparison.
- sorting_order: This is an optional argument. This is being taken care of by the “Reverse” If we wish to sort the iterable in descending order, then its set to be true; else, If we wish to sort the iterable in ascending order, then its set to be false like below:
- sorted (iterable, key, reverse = True)
- sorted (iterable, key, reverse = False)
- If we do not specify the reverse keyword, then by default, the sorting order will be ascending as by default, the reverse keyword is set to false.
Example of Python Sorted Function
Let’s go through the below example:
Code:
List = [ 22 , 33 , 123 , 45 , 45 , 12 , 7 , 4 , 1 , 44 , 98 , 998 , 1 , 11 , 132 , 3445 , 15 , 3 , 5 , 8 , 2 , 4 , 6 , 32 , 45 , 56 , 87 , 111 , 23 , 1 , 3 , 4 , 5 , 8 ]
print ("Sorted list without any optional parameters :", )
print (sorted(List))
print ("\nSorted list with Reverse sort by setting reverse keyword to True:",)
print (sorted(List, reverse = True) )
print ("\nOriginal list without any modifications :",)
print (List)
Output:
How does Python Sorted Function Work?
- Here we have created a list of multiple numerals.
- In the first step [print (sorted(List)) ], we have not utilized any optional argument, so by default, the sorted function returns the sorted list in ascending order.
- In the second step[ print (sorted(List, reverse = True) )], we have also specified the reverse argument and set it to true; that’s why the sorted function returns the sorted list in descending order.
- Whereas in the third step, we are just printing the list as it is.
Example #1
Let’s take another example to understand how is sorting done on Sets?
Code:
# Python program to understand how sorted function works with sets
Set = { 'a' , 's' , 'd' , 'f' , 'g' , 'h' }
print (sorted(Set))
Output:
Example #2
Let’s take another example to understand how is sorting done on Dict.
Code:
# Python program to understand how sorted function works with Dict
Dict= { 'a':1 , 's':2 , 'd':3 , 'f':4 , 'g':5 , 'h':6 }
print (sorted(Dict))
Output:
Example #3
Let’s take another example to understand how is sorting done on the string.
Code:
# Python program to understand how sorted function works with string
str = "Learn Analytics"
print (sorted(str))
Output:
Example #4
Let’s take another example to understand how is sorting done on Tuple.
Code:
# Python program to understand how sorted function works with Tuple
Tuple = ( 'a' , 's' , 'd' , 'f' , 'g' , 'h' )
print (sorted(Tuple))
Output:
Conclusion
- Each and every iterable object can be sorted using Python’s in-built sorted() function.
- The order of sorting can be controlled by passing an optional argument called “reverse”. By default, sorted() functions sorts in ascending order.
- Key is another optional argument that can be utilized with the sorted() function.
Recommended Articles
We hope that this EDUCBA information on “Python Sorted Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.