Sorting HOW TO Release 3.9.5 Guido van Rossum and the Python development team June 01, 2021 Python Software Foundation Email: docs@python.org Contents 1 Sorting Basics 1 2 Key Functions 2 3 Operator Module Functions 3 4 Ascending and Descending 3 5 Sort Stability and Complex Sorts 3 6 The Old Way Using Decorate-Sort-Undecorate 4 7 The Old Way Using the cmp Parameter 4 8 Odd and Ends 5 Python lists have a built-in$_{list.sort()}$ method that modifies the list in-place. There is also a$_{sorted()}$ built-in function that builds a new sorted list from an iterable. In this document, we explore the various techniques for sorting data using Python. A simple ascending sort is very easy: just call the$_{sorted()}$ function. It returns a new sorted list: You can also use the$_{list.sort()}$ method. It modifies the list in-place (and returns$_{None}$ to avoid confusion). Usually it’s less convenient than$_{sorted()}$ - but if you don’t need the original list, it’s slightly more efficient. 1 >>> sorted([5,2,3,1,4]) [1, 2, 3, 4, 5] Author Andrew Dalke and Raymond Hettinger Release 0.1 1 Sorting Basics