Questions on list.sort()

Brian Quinlan brian at sweetapp.com
Thu May 23 22:58:29 EDT 2002


Geiger Ho wrote:
>   I would like to know is there any difference between the following
two
> lines of code:
> 
> 1. mylist.sort()
> 2. mylist.sort(lambda a, b: cmp(os.path.normcase(a),
os.path.normcase(b))
> 
> where mylist = os.listdir('.')

There is a semantic difference on platforms where os.path.normcase is
not a no-op. 

mylist.sort() sorts the list by ASCII order e.g.

>>> mylist = ['a','b','c','A', 'B', 'C']
>>> mylist.sort()
>>> mylist
['A', 'B', 'C', 'a', 'b', 'c']

So the sort order may change if the case changes.

Cheer,
Brian






More information about the Python-list mailing list