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 Overflow
Discussions

python - Case insensitive sorting with sort(list, key=str.lower) - Stack Overflow
I have a module designed to allow users to enter 10 words, then alphabetize them, and display them. Just using the sort functions puts capitalized words first, so i used sort(list, key=str.lower) b... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 12, 2013
sorting - How to sort text strings in Python case-insensitively AND deterministically - Stack Overflow
I have the standard requirement to sort a list of text strings case-insensitively. However additionally this sort needs to be deterministic in the way that two lists containing the same elements sh... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to do case insensitive sort of a dictionary and store them in OrderedDict - Stack Overflow
What I want to do is to perform key sorting using case insensitive and stored them in OrderedDict yielding: More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I make this sorting case insensitive? - Stack Overflow
I have a list with dictionaries. This function allows me to sort them by their first_name. However, it's case-sensitive. More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 8, 2011
People also ask

How do I sort strings case-insensitively in Python?
Use sorted(strings, key=str.lower) or strings.sort(key=str.lower). For international text, use key=str.casefold instead. The key function converts strings to lowercase for comparison only โ€” original capitalisation is preserved in the output. Use sorted() when callers or other code may need the original list unchanged. Use .sort() when the list is owned by the current operation and mutating it is intentional. Both accept reverse=True for descending order.
๐ŸŒ
openpython.org
openpython.org โ€บ home โ€บ articles โ€บ python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically ...
Why does Python sort uppercase before lowercase by default?
Python compares strings using Unicode code points. Uppercase letters (A=65 to Z=90) have lower code point values than lowercase letters (a=97 to z=122), so "Z" < "a" evaluates to True. This is correct Unicode ordering but not human alphabetical order. Use key=str.lower or key=str.casefold to get alphabetical order. String comparison is lexicographic: it compares the first differing code point, then only examines later characters if necessary. That is why the capitalization of the first character can dominate the entire result.
๐ŸŒ
openpython.org
openpython.org โ€บ home โ€บ articles โ€บ python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically ...
How do I sort strings case-insensitively in descending order?
Combine key=str.lower (or casefold) with reverse=True: sorted(words, key=str.lower, reverse=True). This sorts Z to A ignoring case. Do not reverse the characters in each string; descending order reverses item placement, not spelling. If you add a secondary tuple key, test the tie behavior because the chosen direction may affect how ties are perceived.
๐ŸŒ
openpython.org
openpython.org โ€บ home โ€บ articles โ€บ python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ case insensitive tuple sorting by an element
r/learnpython on Reddit: case insensitive tuple sorting by an element
April 12, 2020 -

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.

๐ŸŒ
Programming Idioms
programming-idioms.org โ€บ idiom โ€บ 297 โ€บ sort-a-list-of-strings-case-insensitively โ€บ 5479 โ€บ python
Sort a list of strings, case-insensitively, in Python
func lessCaseInsensitive(s, t string) bool { for { if len(t) == 0 { return false } if len(s) == 0 { return true } c, sizec := utf8.DecodeRuneInString(s) d, sized := utf8.DecodeRuneInString(t) lowerc := unicode.ToLower(c) lowerd := unicode.ToLower(d) if lowerc < lowerd { return true } if lowerc > lowerd { return false } s = s[sizec:] t = t[sized:] } } sort.Slice(data, func(i, j int) bool { return lessCaseInsensitive(data[i], data[j]) })
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-list-sort-method
Python List sort() Method - Learn By Example
December 22, 2022 - If you want to sort the values ... 'Green', 'orange', 'Red'] This causes the sort() function to treat all the list items as if they were lowercase without actually changing the values in the list....
Find elsewhere
๐ŸŒ
OpenPython
openpython.org โ€บ home โ€บ articles โ€บ python case-insensitive sort: how to sort strings alphabetically
Python Case-Insensitive Sort: How to Sort Strings Alphabetically | OpenPython
1 week ago - Learn how to sort Python strings case-insensitively using key=str.lower, str.casefold, and locale-aware sorting. Covers ascending, descending, lists of dicts...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_sort.asp
Python - Sort Lists
Luckily we can use built-in functions as key functions when sorting a list. So if you want a case-insensitive sort function, use str.lower as a key function:
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python short | perform Case Insensitive sort of python list #shorts - YouTube
Learn within a minute how to sort a Python list case insenstively.Watch the entire playlist of Python one-liners to rock your next interviewhttps://www.youtu...
Published: February 23, 2022
Views: 199
๐ŸŒ
Autodesk
help.autodesk.com โ€บ cloudhelp โ€บ CHS โ€บ MayaCRE-Tech-Docs โ€บ CommandsPython โ€บ sortCaseInsensitive.html
sortCaseInsensitive command
Python examples. ... Note: Strings representing object names and arguments must be separated by commas. This is not depicted in the synopsis. sortCaseInsensitive is NOT undoable, NOT queryable, and NOT editable. This command sorts all the strings of an array in a case insensitive way.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Programming 30 - Case Insensitive Sort - YouTube
Start your software dev career - https://calcur.tech/dev-fundamentals ๐Ÿ’ฏ FREE Courses (100+ hours) - https://calcur.tech/all-in-ones๐Ÿ Python Course - https:...
Published: August 30, 2020
๐ŸŒ
ActiveState
code.activestate.com โ€บ recipes โ€บ 286204-case-insensitive-sort
Case Insensitive Sort ยซ Python recipes ยซ ActiveState Code
July 7, 2004 - This is a recipe that does a case insensitive sort. The normal sort methods of lists has 'B'<'a', which means that it would sort 'Pear' to come before 'apple' in a list. You can pass in a function to the sort method to change this... but this can be slow. This is a function that transforms the list, uses the sort method and then transforms it back. ... In the caseless module I am using this to become the sort method for a subclass of list. That class depends on python 2.2 as it subclasses list - but this function ought to work on versions earlier than that.
๐ŸŒ
Python
python-list.python.narkive.com โ€บ WEfapLZl โ€บ case-insensitive-and-internationalized-sort
case-insensitive and internationalized sort
If a named parameter is going to be added to the sort method, it would probably require a PEP and discussion on python-dev before it was accepted. But since sort() doesn't do what a lot of people expect I would like to discuss the issues here first. This topic came out of a off-list discussion I've been having with Jarno J Virtanen, who supplied the following case-insensitive function and test code: def compare(a, b): return cmp(a.upper(), b.upper()) Now, if I test it with the following list: s = [u'?', u'?', u'?', 'b', 'a', 'B', u'a', 'A'] s.sort(compare) for c in s: print c.encode('latin-1'), print it yields: a a A b B ?
๐ŸŒ
Skillsugar
skillsugar.com โ€บ how-to-sort-a-list-alphabetically-in-python
How to Sort a list Alphabetically in Python - SkillSugar
April 28, 2021 - The Python sorted() function takes an optional second argument key which we can use to make a case insensitive sort.
๐ŸŒ
JetBrains
youtrack.jetbrains.com โ€บ issue โ€บ PY-20159
"Sort import statements" should be case-insensitive
December 2, 2018 - Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions.
๐ŸŒ
Python
python-list.python.narkive.com โ€บ UQVb86Le โ€บ sort-list-of-dictionaries-by-key-case-insensitive
Sort list of dictionaries by key (case insensitive)
Unfortunately, I only have Python 2.3.5 installed and can't upgrade to 2.4 due to an underliying application server. In python 2.3 the 'sort()' function does not excepts any keywords arguments (TypeError: sort() takes no keyword arguments), so is there a workaround?
๐ŸŒ
Coding
studycode3.wordpress.com โ€บ 2023 โ€บ 03 โ€บ 31 โ€บ 65-case-insensitive-sorting-in-python
65. Case-Insensitive Sorting in python โ€“ Coding
March 31, 2023 - Case-Insensitive Sorting in python refers to a way of sorting strings in a case-insensitive manner, which means that the sorting algorithm treats uppercase and lowercase letters as equivalent.