sorting a list

Peter Otten __peter__ at web.de
Tue Mar 9 18:35:48 EST 2004


ketulp_baroda at yahoo.com wrote:

> 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?

Assuming you want to sort ascii alphabetically with uppercase before
lowercase:

>>> a = list('afFAD')
>>> def icmp(s, t):
...     return cmp(s.lower(), t.lower()) or cmp(s, t)
...
>>> a.sort(icmp)
>>> a
['A', 'a', 'D', 'F', 'f']
>>>

Peter



More information about the Python-list mailing list