[Tutor] sort method

Sean 'Shaleh' Perry shalehperry@attbi.com
Tue, 1 Oct 2002 09:48:57 -0700


On Tuesday 01 October 2002 09:24, Annika Scheffler wrote:
> Hi,
>
> I am quite new to Python and I need your help:
>
> Can you give me a very simple example of the list.sort method?
> For instance I would like to sort this list:
>
> l =3D "We all place a great deal of reliance on the Theory and Practice=
 of
> science, but the hopeful intentions of so many inventions can be quite
> buggered up in appliance.".split()
>
> How do I use the sort method?
>
> If I simply write l.sort() , it doesn't work...what do I need to add?
>
> Thanks a lot,
> Annika

1) l.sort() sorts the list 'l'.  sort() does not return a new, sorted lis=
t.
2) if you are unhappy with the order you get from sort() you can pass it =
a=20
function which takes two arguments, compares them and returns either -1, =
0,=20
or 1.  There is a builtin function call cmp() which is often used as a=20
building block.

def insensitive_cmp(left, right):
    return cmp(left.lower(), right.lower())

l.sort(insensitive_cmp)