[Tutor] sort() once more...

Sean 'Shaleh' Perry shalehperry@attbi.com
Wed, 2 Oct 2002 10:10:23 -0700


On Wednesday 02 October 2002 10:04, Annika Scheffler wrote:
> Hi,
>
> thanks for everyone for your help!
> I'm afraid I have another question...:
>
> For example, I have a list like this: l =3D "What Do we have here".spli=
t().
> If I use the sort() method on this list I get:
> ['Do', 'What', 'have', 'here', 'we']
> What I want as a result is this, though:
>
> ['Do', 'have', 'here', 'we', 'What']
>
> In other words, I'd like the sort() method to ignore the first letter o=
f
> each word being lower or uppercase. How can I do that?
>
> Thanks a lot,
> Annika

As I said in my first response:

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)

This does what you need here.  Or does it really matter that:

aBc is sorted differently from ABC?  If so you could just lower case the =
first=20
letter inside the function.