sorting a list

Paul McGuire bogus at bogus.net
Tue Mar 9 18:23:02 EST 2004


<ketulp_baroda at yahoo.com> wrote in message
news:f046efac.0403091513.3b392074 at posting.google.com...
> Hi
> I want to sort a list.
> My problem is:
>
> >>> a=['a','f','F','A','D']
> >>> a.sort()
> >>> a
> ['A','D', 'F', 'a', 'f']
>
>
> But I am actually looking for an output:
> ['A','a','D','F','f']
>
> Is there any module to sort a list this way?


>>> a =['a','f','F','A','D']
>>> a.sort( lambda x,y: (x.upper() == y.upper() and ord(x)-ord(y) or
ord(x.upper())-ord(y.upper()) ))
>>> a
['A', 'a', 'D', 'F', 'f']

(could probably stand a little more rigorous testing, though...)

-- Paul





More information about the Python-list mailing list