We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Reuven Lerner - How to sort anything
Learn how to master sorting in Python with Reuven Lerner, including custom key functions, stable sorting, and implementing total ordering concepts.
- Sorting is important and is done frequently in programming
- 
Use sorted()function with a custom key function to sort lists
- A key function can be a lambda function or a regular function
- Key functions can sort lists of strings, numbers, and objects
- 
sorted()method modifies the original list if the key function returns a mutable object
- 
To avoid modifying the original list, use sorted()function with a key function that returns an immutable object
- The default sort order is ascending, but descending order can be achieved by multiplying the key function’s return value by -1
- 
The operator.itemgetterfunction can be used to create a key function that sorts lists of objects
- 
Data classes and the @dataclassdecorator can be used to create classes with immutable objects
- 
The @dataclassdecorator automatically generates methods for sorting and comparing objects
- 
Total ordering can be achieved by implementing __lt__,__eq__, and__gt__methods in a class
- 
Python’s sorted()function is stable, meaning that it maintains the relative order of equal elements
- 
The sorted()function is customizable and can be used with lists, tuples, dictionaries, and other iterable objects
- Tim Sort, a stable sorting algorithm, is the default sorting algorithm used in Python
- Tim Sort is fast and efficient, and is suitable for sorting large lists of objects