Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
4. Sorting in Python
Sorting – sorted()
Sorting – reverse() and reversed()
Summary

Instruction

Okay, let's move on to sorting. The sort problem (i.e., putting elements in a certain order) is another building block of computer science. Most real-world applications use some kind of sorting.

Writing a proper sort algorithm is not as easy as it may seem. Different algorithms – such as merge sort, heap sort, or quick sort – are used for sorting different data structures. Still, sorting is always an expensive operation in terms of performance, which is something to keep in mind.

As you can probably guess, Python provides a ready-made sorting function for you. And it's pretty easy to use:

list_name.sort()

Why not give it a try?

Exercise

Take a look at the template code. You can see an unsorted list of books. The code prints the books variable after using the sort() function. Run the code and see what happens.

As you can see, the books are now sorted alphabetically by title.