#Using the sorted() function

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

print(sorted(L))

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

sorted(iterable, reverse = False, key = None)
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. 

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

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)

Sorting elements in a list of strings

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

sorted_L = sorted(L)

print(sorted_L)

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)

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

sorting  tuple elements

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

sorted_T = sorted(T)
print(sorted_T)

Sorting in reverse

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

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)

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)

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.

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)

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.

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)

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. 

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)

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)

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)

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.