Random832 wrote: > On Mon, Apr 4, 2016, at 02:56, Peter Otten wrote: >> > That works well. Why is it 'cheating'? >> >> On second thought it isn't ;) > > It does require a numeric type, though. There are lots of types that are > orderable but do not have a negation operator that provides a key with > reversed ordering.
If you are willing to accept the overhead you can use a wrapper class: >>> import functools >>> @functools.total_ordering ... class neg: ... def __init__(self, key): ... self.key = key ... def __lt__(self, other): ... return other.key < self.key ... >>> sorted("abcde", key=neg) ['e', 'd', 'c', 'b', 'a'] -- https://mail.python.org/mailman/listinfo/python-list