list.sort(cmpfunc) question

Simon Budig Simon.Budig at unix-ag.org
Fri Mar 16 18:26:42 EST 2001


Aahz Maruch <aahz at panix.com> wrote:
> In article <m1ae6lg5qt.fsf at pclabs188.labs.trema.com>,
> Harri Pasanen  <harri at trema.com> wrote:
>>
>>The list.sort(cmp) docs for Python 2.0 state:
>>
>>   The sort() method takes an optional argument specifying a
>>   comparison function of two arguments (list items) which should
>>   return -1, 0 or 1 depending on whether the first argument is
>>   considered smaller than, equal to, or larger than the second
>>   argument.
> 
> Note that "should" carefully.  The current implementation of sort() only
> checks for a negative value, but that's not a behavior that should be
> relied on in your coding.  You can be sure that any anamolies in the
> behavior of cmp() will be fixed if the sort() algorithm ever changes.

As a related question since I did not find it in the documentation:

Is the sort-function guaranteed to be stable? Does it keep the order,
when cmpfunc returns 0 for a pair of items?

Thanks,
        Simon
-- 
      Simon.Budig at unix-ag.org       http://www.home.unix-ag.org/simon/



More information about the Python-list mailing list