[Python-Dev] decorate-sort-undecorate

Scott David Daniels Scott.Daniels at Acm.Org
Mon Oct 13 20:40:35 EDT 2003

Raymond Hettinger wrote:

>For Py2.4, I propose adding an optional list.sort() argument to
>support the decorate-sort-undecorate pattern.

>def sort(self, cmpfunc=None, key=None):
>    ...
>    if key is None:
>        self.sort(*args)
>    else:      
>        aux = zip(map(key, self), self)   # Decorate
>        aux.sort(*args)
>        self[:] = list(zip(*aux)[1])       # Un-decorate
If the argument is for simplicity, do we need to make this stable?
Will warning about incomparables be sufficient?  I'm thinking
    data = [(1-1j), -2, 1, 1j]

Or would we prefer the code to end:

        # Decorate
        aux = [(key(el), nbr, el) for nbr, el in enumerate(self)]
        self[:] = list(zip(*aux)[2])       # Un-decorate

I think the answer comes down to performance vs. law of least
surprise.  I suppose I am slightly in favor of throwing in the
stabilizing count (fewer explanations; those who need speed can
do it themselves.

-Scott David Daniels
 Scott.Daniels at Acm.Org

More information about the Python-Dev mailing list