Sorted () function in Python

Python Methods and Functions

Syntax: sorted (repeatable, key, reverse)

Parameters: sort takes three parameters, of which two are optional.

  • Iterable: a sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
  • Key (optional): A function that will serve as a key or base for collation comparison by the server.
  • Reverse (optional): if set to true, then the iteration will be sorted in reverse (descending) order, by default it is set to false.

Example 1

x = [ 2 , 8 , 1 , 4 , 6 , 3 , 7 ]

 

print "Sorted List returned:" ,

print sorted (x)

 

print "Reverse sort:" ,

print sorted (x, reverse = True )

  

print " Original list not modified: " ,

print x

  Output:  Sorted List returned: [1, 2, 3, 4, 6 , 7, 8] Reverse sort: [8, 7, 6, 4, 3, 2, 1] Original list not modified: [2, 8, 1, 4, 6, 3, 7] 

Example 2: sorting different data types

# List

x = [ ' q' , 'w' , ' r ' , ' e' , ' t' , 'y' ]

print sorted (x)

 
# Tuple

x = ( ' q' , 'w' , ' e' , 'r' , ' t' , 'y' )

print sorted (x)

 
# Sort strings based on ASCII translations

x = "python"

print sorted (x)

 
# Glossary

x = { 'q' : 1 , 'w' : 2 , 'e ' : 3 , ' r' : 4 , 't' : 5 , ' y' : 6 }

print sorted (x)

 
# Install

x = { 'q' , 'w' , ' e' , 'r' , 't' , ' y' }

print sorted (x)

 
# Frozen Set

 x = frozenset (( ' q' , 'w' , 'e' , ' r' , ' t' , 'y' ))

print sorted (x)

  Output :  ['e',' q', 'r',' t', 'w',' y'] ['e',' q', 'r',' t', 'w' , 'y'] [' h', 'n',' o', 'p',' t', 'y'] [' e', 'q',' r', 't',' w' , 'y'] [' e', 'q',' r', 't',' w', 'y'] [' e', 'q',' r', 't',' w' , 'y'] 

Selective sorting by key parameter
The sorted () function has an optional key parameter that accepts a function as a value. This key function converts each element before sorting, it takes a value and returns 1 value, which is then used in the sort instead of the original value.

For example, if we pass a list of strings to sorted (), it is sorted alphabetically. But if we specify key = len, i.e. we give len as a key, the strings will be passed to len, and the value it returns, i.e. the length of the lines will be sorted. This means the lines will be sorted by length instead of

L = [ "cccc" , "b" , "dd" , "aaa" ]

 

print "Normal sort:" , sorted (L)

 

print "Sort with len:" , sorted (L, key = len )

  Output:  Normal sort: ['aaa',' b', 'cccc',' dd'] Sort with len: ['b',' dd', 'aaa',' cccc'] 

The key also accepts custom functions as the value to sort.

# Sort the list of integers based on
# their remainder from 7

  

def func (x):

return x % 7

 

L = [ 15 , 3 , 11 , 7 ]

 

print "Normal sort:" , sorted (L)

print " Sorted with key: " , sorted (L, key = func)

  Output:  Normal sort: [3, 7, 11, 15] Sorted with key: [7, 15, 3, 11] 

This article is provided by Harshit Agrawal . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.





Tutorials