sort
Sorts a list. Takes one or two arguments, the first being a list
and the second being a function
or None
. The function should take the keyword argument key
. If None
is passed as the second argument, the function will sort by identity.
a = sort([4, 1, 7, 10, 5]) # b = [1, 4, 5, 7, 10]
b = sort([4, 1, 7, 10, 5], None) # a = [1, 4, 5, 7, 10]
def key_fn(key=''):
return key['name']
c = sort([
{'name': 'John'},
{'name': 'Bob'},
{'name': 'Charles'},
], key_fn) # c = [{'name': 'Bob'}, {'name': 'Charles'}, {'name': 'John'}]