The default compare function used when sorting a list of strings uses the ordinal value of the string characters for comparison. This results in the typical ASCII sort result of 'B' < 'a'. If a user is going to see the list, it is generally better to do a case-insensitive comparison.
1 2 3 4 5 6 7 8 9
def caseinsensitive_sort(stringList): """case-insensitive string comparison sort doesn't do locale-specific compare though that would be a nice addition usage: stringList = caseinsensitive_sort(stringList)""" tupleList = [(x.lower(), x) for x in stringList] tupleList.sort() return [x for x in tupleList]
While you can provide your own compare method to pass to the sort method such as:
def compare(a, b): return cmp(a.lower(), b.lower())
or use a lambda
stringList.sort(lambda a, b: cmp(a.lower(), b.lower()))
this results in the lower() conversion for every comparison. Thanks to Martin v. Loewis for making this all clear.