list.sort(cmpfunc) question
Simon Budig
Simon.Budig at unix-ag.org
Fri Mar 16 20:56:58 EST 2001
Tim Peters <tim.one at home.com> wrote:
> [Simon Budig]
>> ...
>> Is the sort-function guaranteed to be stable? Does it keep the order,
>> when cmpfunc returns 0 for a pair of items?
>
> No, Python's list.sort() is not stable. Stability can be "faked", though, by
> sorting a derived list y, where each element y[i] == (x[i], i). Then no two
> list elements are equal. Because tuples are compared lexicographically,
> cmp((x, i), (x, j)) has the same sense as cmp(i, j), thus preserving the
> original order of equal elements in x.
Thanks, that was helpful.
Bye,
Simon
--
Simon.Budig at unix-ag.org http://www.home.unix-ag.org/simon/
More information about the Python-list
mailing list