ExampleEdit & Run
#Using the sorted() function

L = [5, 2, 1, 3, 4]

print(sorted(L))
copy
Output:
[1, 2, 3, 4, 5] [Finished in 0.010585262905806303s]

The sorted() function is used to get a sorted list from  the elements of a given iterable. 

Syntax:
sorted(iterable, reverse = False, key = None)
copy
iterable The iterable containing the elements to be sorted
reverse An optional boolean value(True or False) indicating whether to sort the elements in reverse order. It defaults to False.
key An optional parameter representing a function to be used as the criteria for sorting. 
Parameters:

The function returns a list containing the sorted elements of the given iterable.

By default the function sorts the elements in ascending order i.e from smallest to largest, if we specify  the optional reverse argument as True, the elements will be sorted in descending order.  

Example with a list

ExampleEdit & Run

Sorting elements in a list of integers

#Sort a list of integers.
L = [39, 32, 91, 3, 66, 46, 8, 48, 28, 77]

#sort the elements
sorted_list = sorted(L)

#print the sorted list
print(sorted_list)
copy
Output:
[3, 8, 28, 32, 39, 46, 48, 66, 77, 91] [Finished in 0.009824709966778755s]
ExampleEdit & Run

Sorting elements in a list of strings

L = ['Tokyo', 'Berlin', 'Rio', 'Moscow', 'Nairobi', 'Lisbon', 'Helsinki']

sorted_L = sorted(L)

print(sorted_L)
copy
Output:
['Berlin', 'Helsinki', 'Lisbon', 'Moscow', 'Nairobi', 'Rio', 'Tokyo'] [Finished in 0.010410638060420752s]
ExampleEdit & Run

Sorting elements in a  list of lists

L = [[7, 3], [4, 2], [9, 1], [6, 0], [5, 8]]

sorted_L = sorted(L)
print(sorted_L)
copy
Output:
[[4, 2], [5, 8], [6, 0], [7, 3], [9, 1]] [Finished in 0.009920626878738403s]

In cases where the iterable to be sorted is a list, as in above, you can use the sort()  method to sort the list in place. This returns the original list with the elements sorted , unlike the sorted() function which returns an entirely new list.

Example with tuples

ExampleEdit & Run

sorting  tuple elements

T = (77, 27, 58, 3, 89, 32, 81, 20, 78, 12)

sorted_T = sorted(T)
print(sorted_T)
copy
Output:
[3, 12, 20, 27, 32, 58, 77, 78, 81, 89] [Finished in 0.009868342895060778s]

Sorting in reverse

By specifying the reverse argument as True, the elements will be sorted in reverse order.

ExampleEdit & Run

Sort list elements in reverse order.

L = [[7, 3], [4, 2], [9, 1], [6, 0], [5, 8]]

reverse_sorted_L = sorted(L, reverse = True)
print(reverse_sorted_L)
copy
Output:
[[9, 1], [7, 3], [6, 0], [5, 8], [4, 2]] [Finished in 0.009456837084144354s]
ExampleEdit & Run

Sort tuple elements in reverse

T = (77, 27, 58, 3, 89, 32, 81, 20, 78, 12)

reverse_sorted_T = sorted(T, reverse = True)
print(reverse_sorted_T)
copy
Output:
[89, 81, 78, 77, 58, 32, 27, 20, 12, 3] [Finished in 0.009538485202938318s]

Sorting keys

The optional argument, key ,can be used to specify the sorting criteria. The key is typically a function which  takes a single argument and  gets called on each element in the iterable prior to making comparisons, the returned values are used instead of the actual elements. For example to sort items in an iterable according to their length, we can pass the len() function as the key.

ExampleEdit & Run

Sort strings in a list based on  their length

my_list = ['aa', 'eee', 'ccccc', 'b', 'dddd', 'fffff']

#Pass the builtin len() function as the key
sorted_list = sorted(my_list, key = len)

print(sorted_list)
copy
Output:
['b', 'aa', 'eee', 'dddd', 'ccccc', 'fffff'] [Finished in 0.009340605698525906s]

The following example uses the abs() function as the key to sort integers in a list based on their absolute values rather than their natural order.

ExampleEdit & Run

Sort integers based on theirs absolute value

L = [-7, 0, 10, 4, -4, -6, 2, -8, 9, 3]

sorted_L = sorted(L, key = abs)
print(sorted_L)
copy
Output:
[0, 2, 3, 4, -4, -6, -7, -8, 9, 10] [Finished in 0.009315587114542723s]

Lambda functions are incredibly versatile and they can be really useful in creating short and frexible one-line functions that can be passed as keys to sorted() and similar functions. 

ExampleEdit & Run

Sort all even numbers before odd numbers

L = [i for i in range(15)]

sorted_L = sorted(L, key = lambda x: x % 2)

print(sorted_L)
copy
Output:
[0, 2, 4, 6, 8, 10, 12, 14, 1, 3, 5, 7, 9, 11, 13] [Finished in 0.009245852008461952s]
ExampleEdit & Run

Sort a tuple based on its second argument

L = [('Python', 2), ('Javascript', 1), ('Java', 4), ('C++', 3)]

sorted_L = sorted(L, key = lambda x: x[1])

print(sorted_L)
copy
Output:
[('Javascript', 1), ('Python', 2), ('C++', 3), ('Java', 4)] [Finished in 0.009871663060039282s]
ExampleEdit & Run

All the lists which includes "Python" will come first

L =[['Java', 'Python', 'Go'], ['Ruby', 'C++', 'Scala'], ['Python', 'R', 'Kotlin'], ['C#', 'PHP', 'TypeScript'], ['Swift', 'Python', 'Rust']]

sorted_L = sorted(L , key = lambda x : 'Python' in x , reverse = True)

print(sorted_L)
copy
Output:
[['Java', 'Python', 'Go'], ['Python', 'R', 'Kotlin'], ['Swift', 'Python', 'Rust'], ['Ruby', 'C++', 'Scala'], ['C#', 'PHP', 'TypeScript']] [Finished in 0.00951805291697383s]

Conclusion

You should use the sorted function to quickly get a list of sorted elements from an iterable.

The performance of the sorted() function is quite fast and efficient. It uses the Timsort algorithm which is a combination of insertion sort and merge sort. The Timsort algorithm is very efficient in sorting already partially sorted data.