Questions on list.sort()

David Bolen db3l at fitlinxx.com
Thu May 23 23:08:39 EDT 2002


Geiger Ho <s997659 at ee.cuhk.edu.hk> writes:

> Hi all,
> 
>   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('.')

Depends on whether os.path.normcase() would change the case of any of
the names returned by os.listdir('.').  The default sort() isn't going
to change the contents of the list at all, but simply compare the
strings literally.  So if you have some uppercase or mixed case
filenames then the lambda version would sort differently.

If you're asking if the default comparison function is case
insensitive for strings, it isn't.  E.g.:

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> cmp('A','a')
-1

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list