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']
Answer from user25148 on Stack OverflowIn 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']
>>> x = ['Aden', 'abel']
>>> sorted(x, key=str.lower) # Or unicode.lower if all items are unicode
['abel', 'Aden']
In Python 3 str is unicode but in Python 2 you can use this more general approach which works for both str and unicode:
>>> sorted(x, key=lambda s: s.lower())
['abel', 'Aden']
python - Case insensitive sorting with sort(list, key=str.lower) - Stack Overflow
sorting - How to sort text strings in Python case-insensitively AND deterministically - Stack Overflow
python - How to do case insensitive sort of a dictionary and store them in OrderedDict - Stack Overflow
python - How do I make this sorting case insensitive? - Stack Overflow
How do I sort strings case-insensitively in Python?
Why does Python sort uppercase before lowercase by default?
How do I sort strings case-insensitively in descending order?
EDIT: Thanks to SHxKM and cybersection for pointing out that I called lower and not lower(). Very different and the latter fixed my issue.
So I am having a bit of a weird issue here and the normal methods I'm seeing online don't seem to work out.
So I've got a big unsorted list of tuples that I need to sort. For the sake of example let's say that the first element is file mode and the second is the filename. I want the directories on top and the files on the bottom. So I create 2 empty lists (dirlist and filelist) and used this sort of thing:
for i, key in enumerate(unsorted_list):if key[0] == "040000":dirlist.append(formatted_content[i])else:filelist.append(formatted_content[i])
So to combine these into one list I just use something like:
sorted_list = dirlist + filelist
That puts the directories on top and the file on the bottom fine, but they're not sorted, and an alphabetical list would be nice.
So I tried inserting this before the sorted_list code above:
dirlist.sort(key=lambda tup: tup[1])filelist.sort(key=lambda tup: tup[1])
Well, that does, indeed, sort in a manner of speaking, but it's case sensitive, so all the capitalized files floated to the top and THAT is my problem.
Sorting case insensitive is usually done with a lambda, and I've already got one.
Initially I thought about sorting by the key and then sorting by the other key, but, of course, all that does it sort it twice.
I also tried adding a lower to the tup sorting like:
filelist.sort(key=lambda tup: tup[1].lower)
That certainly does SOMETHING, but it isn't alphabetizing, that's for sure (near as I can tell it's putting the lower cased stuff on top, but now really doing any sorting beyond that?).
Anyway, I have been spinning my wheels on this for the better part of four hours, so I thought I'd poke in here and ask for some better guidance.
sorted returns the sorted list. It does not modify the list in place. You'll have to store the sorted list somewhere
words = sorted(words, key=str.lower)
On python 2.6
>>> words= ['Aardvark', 'coke', 'Desk', 'Zippy', 'zappy', 'Television', 'brothel', 'book', 'Dad', 'dog']
>>> sorted(words,key=str.lower)
['Aardvark', 'book', 'brothel', 'coke', 'Dad', 'Desk', 'dog', 'Television', 'zappy', 'Zippy']
>>> words
['Aardvark', 'coke', 'Desk', 'Zippy', 'zappy', 'Television', 'brothel', 'book', 'Dad', 'dog']
>>> words = sorted(words,key=str.lower)
>>> words
['Aardvark', 'book', 'brothel', 'coke', 'Dad', 'Desk', 'dog', 'Television', 'zappy', 'Zippy']
sorted(words, key=cmp_to_key(locale.strcoll))
You could first sort them with case sensitivity, and then sort again ignoring case
>>> sorted(sorted(l1), key=str.casefold)
['alfred', 'Berta', 'berta', 'carl']
>>> sorted(sorted(l2), key=str.casefold)
['alfred', 'Berta', 'berta', 'carl']
You can use a key function that returns a tuple with the case-folded string and then the original string. This will cause the case-folded strings to be compared first, making the sort case-insensitive. Then if the case-folded strings are identical, the original strings will be compared, ensuring that the result is deterministic.
def deterministic_casefold(s):
return s.casefold(), s
sorted(l1, key=deterministic_casefold)
sorted(l2, key=deterministic_casefold)
>>> from operator import itemgetter
>>> p = [{'fn':'bill'}, {'fn':'Bob'}, {'fn':'bobby'}]
>>> sorted(p, key=itemgetter('fn'))
[{'fn': 'Bob'}, {'fn': 'bill'}, {'fn': 'bobby'}]
>>> sorted(p, key=lambda x: x['fn'].lower())
[{'fn': 'bill'}, {'fn': 'Bob'}, {'fn': 'bobby'}]
>>>
Here's a way:
return sorted(p, key=lambda x: x['first_name'].lower())