Sorting list (maybe stupid)

James Kew james.kew at btinternet.com
Tue Jun 11 18:52:06 EDT 2002


"Chris Barker" <Chris.Barker at noaa.gov> wrote in message
news:3D062B7C.FE6A4963 at noaa.gov...
> Eric Brunel wrote:
>
> > l = [(102, 'foo'), (2, 'bar'), (1, 'baz')]
> > l.sort()
> > print l
> >
> > Apparently, the default sort function already sorts tuples according to
> > their first element...
>
> Note that this only works if the rest of the elements in the tuples are
> also sortable. In this case, they are strings, so they are.

Although that does mean that, in the OP's problem, lines with identical
numbers will be further sorted by the following string -- this might not be
the behaviour he requires.

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






More information about the Python-list mailing list