How to Sort a List in Python
Author: Newtum
This guide covers the two main ways to sort a list in Python: the `list.sort()` method, which sorts the list in-place, and the `sorted()` function, which returns a new sorted list.
`list.sort()`: In-Place Sorting
The `.sort()` method modifies the list directly and returns `None`. It's efficient if you don't need to keep the original, unsorted list.
`sorted()`: Returning a New Sorted List
The built-in `sorted()` function takes any iterable as input and returns a new, sorted list. The original list remains unchanged.
Sorting with a Custom Key
Both methods accept a `key` argument, which is a function that returns a value to sort by. This is powerful for sorting complex objects.