Q: sort's key and cmp parameters

Scott David Daniels Scott.Daniels at Acm.Org
Fri Oct 2 10:38:07 EDT 2009


Paul Rubin wrote:
> I still have never understood why cmp was removed.  Sure, key is more
> convenient a lot (or maybe most) of the time, but it's not always.

Not just more convenient.  cmp will always be N log N, in that _every_
comparison runs your function, while key is linear, in that it is run
once per element.  Most cases are moreeasily done with key, and it is
a good idea to make the most accessible way to a sort be the most
efficient one.  In the rare case that you really want each comparison,
the cmp-injection function will do nicely (and can be written as a
recipe.

In short, make the easy path the fast path, and more will use it;
provide two ways, and the first that springs to mind is the one
used.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list