Python 3 removes the cmp
parameter to sorting functions:
builtin.sorted()
andlist.sort()
no longer accept thecmp
argument providing a comparison function. Use thekey
argument instead.
That's fine for orderings that can be determined just by inspecting a single item in the sequence (e.g. key=str.lower
). But what about custom orderings that must have two items for inspection to determine their ordering?
$ python2
Python 2.7.12+ (default, Sep 1 2016, 20:27:38)
[…]
>>> digits = ['3', '30', '34', '32', '9', '5']
>>> sorted(
... digits,
... cmp=(lambda a, b: cmp(a+b, b+a)),
... reverse=True)
['9', '5', '34', '3', '32', '30']
That two-parameter cmp
function can't be replaced with a one-parameter key
function, can it?