Sorting a list based on a characteristic their elements
sorted() has an optional keyparameter that can be used to sort the elements based on a given characteristic rather than the elements themselves.
In the following example, key transforms each element to their uppercase equivalent to sort strings alphabetically. Without using the key parameter, uppercase letters are sorted before lowercase letters:
words = 'one', 'TWO', 'three', 'four', 'Five'
for word in sorted(words):
print(word)
print('')
for word in sorted(words, key = lambda w: w.upper()):
print(word)
If __lt__ hadn't been defined, sorting these object would have resulted in a TypeError exception (TypeError: '<' not supported between instances of 'CLS' and 'CLS').