[Python-Dev] decorate-sort-undecorate

Just van Rossum just at letterror.com
Thu Oct 16 07:19:06 EDT 2003


Raymond Hettinger wrote:

> If the discussion is wrapped up, I'm ready to commit the patch:
> 
>    www.python.org/sf/823292
> 
> Summary:
> 
> .. Adds keyword arguments:  cmp, key, reverse.
> .. Stable for any combination of arguments (including reverse).
[ ... ]

On the sf tracker item you write:

    def sort(self, cmp=None, key=None, reverse=None):
        if cmp is not None and key is not None:
            cmp = cmpwrapper(cmp)
        if key is not None:
            self[:] = [sortwrapper(key(x), x) for x in self]
        if reverse is not None:
            self.reverse()        
        self.sort(cmp)
        if key is not None:
            self[:] = [x.getvalue() for x in self]
        if reverse is not None:
            self.reverse() 

Is there consensus at all about the necessity of that first reverse
call? To me it's not immediately obvious that the reverse option should
maintain the _original_ stable order. In my particular application I
would actually want reverse to do just that: reverse the result of the
sort. Easy enough to work around of course: I could do the reverse
myself after the sort. But it does feel odd: sort() now _has_ a reverse
feature, but I can't use it...

(Also: how does timsort perform when fed a (partially) sorted list
compared to a reversed sorted list? If there's a significant difference
there, than that first reverse call may actually hurt performance in
some cases. Not that I care much about that...)

Just



More information about the Python-Dev mailing list