In which versions is list.sort stable?

Dan Bishop danb_83 at yahoo.com
Sun Apr 27 16:39:44 EDT 2003


Beni Cherniavsky <cben at techunix.technion.ac.il> wrote in message news:<mailman.1051467524.10087.python-list at python.org>...

> Why don't we just grow a `list.stablesort` method that can be an alias
> to `list.sort` currently but is guaranteed to be some stable sort
> forever?  Explicit is better than implicit, so in the rare case that
> you do rely on it, why not spell it out?

Or, as a temporary workaround:

# non-in-place sort
def stablesort(lst):
   # insure stable sorting by adding indices to the list to sort
   lst = [(lst[i], i) for i in xrange(len(lst))]
   lst.sort()
   # remove the indices
   return [t[0] for t in lst]

# in-place sort
def stablesort(lst):
   for i in xrange(len(lst)):
      lst[i] = (lst[i], i)
   lst.sort()
   for i in xrange(len(lst)):
      lst[i] = lst[i][0]




More information about the Python-list mailing list