Sorting list (maybe stupid)

Ken Seehof kseehof at neuralintegrator.com
Wed Jun 12 01:58:34 CEST 2002


> Is sort() stable -- that is, is the relative order of elements 
> which compare
> equal preserved? A quick shufti through the ActivePython docs wasn't
> illuminating.
> 
> --
> James Kew
> james.kew at btinternet.com

Documentation?  Real programmers don't read (or write)
documentation. :-)

>>> a = [1,1.0,3.0,3,2.0,2,0,0.0]
>>> a
[1, 1.0, 3.0, 3, 2.0, 2, 0, 0.0]
>>> a.sort()
>>> a
[0, 0.0, 1, 1.0, 2.0, 2, 3.0, 3]

So yes, sort() is stable.

I suppose it's theoretically possible for a different
python implementation to use a different sort algorithm,
but practically speaking, that is very unlikely.

- Ken Seehof







More information about the Python-list mailing list