Search code examples
pythonstringlistsortingcase-insensitive

case-insensitive list sorting, without lowercasing the result?


I have a list of strings like this:

['Aden', 'abel']

I want to sort the items, case-insensitive. So I want to get:

['abel', 'Aden']

But I get the opposite with sorted() or list.sort(), because uppercase appears before lowercase.

How can I ignore the case? I've seen solutions which involves lowercasing all list items, but I don't want to change the case of the list items.


Solution

  • In Python 3.3+ there is the str.casefold method that's specifically designed for caseless matching:

    sorted_list = sorted(unsorted_list, key=str.casefold)
    

    In Python 2 use lower():

    sorted_list = sorted(unsorted_list, key=lambda s: s.lower())
    

    It works for both normal and unicode strings, since they both have a lower method.

    In Python 2 it works for a mix of normal and unicode strings, since values of the two types can be compared with each other. Python 3 doesn't work like that, though: you can't compare a byte string and a unicode string, so in Python 3 you should do the sane thing and only sort lists of one type of string.

    >>> lst = ['Aden', u'abe1']
    >>> sorted(lst)
    ['Aden', u'abe1']
    >>> sorted(lst, key=lambda s: s.lower())
    [u'abe1', 'Aden']