Sort a List With Lowercase First

Python sort strings alphabetically, lowercase first

Use a custom key method which checks whether the item is not .lower() and then compares the items itself. For 'A', 'D' and 'B' not x.islower() will return True and for other it is False, as True > False smaller case items will come first:

>>> arr = ['A','e','a','D','f','B']
>>> arr.sort(key=lambda x:(not x.islower(), x))
>>> arr
['a', 'e', 'f', 'A', 'B', 'D']

Sort a list with lowercase first

This is what I was looking for: sort -t ';' -k 2,2 < some-csv.log

Big thanks to @dmadic

How to sort a list in python to make lowercase precede uppercase?

You can chain two sorts like this, since Python's sort is stable

>>> sorted(sorted(['Bear', 'bear', 'Apple', 'apple'], reverse=True), key=str.lower)
['apple', 'Apple', 'bear', 'Bear']

Or you can use a single sort, like this:

Python2:

>>> sorted(['Bear', 'bear', 'Apple', 'apple'], key=lambda x: (x.lower(), x.swapcase()))
['apple', 'Apple', 'bear', 'Bear']

Python3:

>>> sorted(['Bear', 'bear', 'Apple', 'apple'], key=lambda x: (x.casefold(), x.swapcase()))
['apple', 'Apple', 'bear', 'Bear']

How to sort a list to get all lowercase alphabet words before uppercase?

Try this:

sorted(['aa', 'bb', '_dd', 'BB', 'AA'], key=lambda s: (s.upper() == s, s))

Tuples are sorted by its first item before the second, and bools are sorted as False before true, so the s.upper() == s will sort all non-uppercase ones before the others.

Note that, if your purpose is language-oriented sorting, you should probably look into using key=functools.cmp_to_key(locale.strcoll) or similar instead.

Python: How do you make a list start with the lowercase strings?

You can pass a custom key function to sort with.

words.sort(key=lambda s: (s[0].isupper(), s))

Python 3 sort list - all entries starting with lower case first

Try this:

>>> l = ['B', 'b','a','A', 'aA', 'Aa','C', 'c']
>>> sorted(l, key=str.swapcase)
['a', 'aA', 'b', 'c', 'A', 'Aa', 'B', 'C']

EDIT:

A one-liner using the list.sort method for those who prefer the imperative approach:

>>> l.sort(key=str.swapcase)
>>> print l
['a', 'aA', 'b', 'c', 'A', 'Aa', 'B', 'C']

Note:

The first approach leaves the state of l unchanged while the second one does change it.

Sort list of strings ignoring upper/lower case

The sort() method and the sorted() function take a key argument:

var.sort(key=lambda v: v.upper())

The function named in key is called for each value and the return value is used when sorting, without affecting the actual values:

>>> var=['ant','bat','cat','Bat','Lion','Goat','Cat','Ant']
>>> sorted(var, key=lambda v: v.upper())
['ant', 'Ant', 'bat', 'Bat', 'cat', 'Cat', 'Goat', 'Lion']

To sort Ant before ant, you'd have to include a little more info in the key, so that otherwise equal values are sorted in a given order:

>>> sorted(var, key=lambda v: (v.upper(), v[0].islower()))
['Ant', 'ant', 'Bat', 'bat', 'Cat', 'cat', 'Goat', 'Lion']

The more complex key generates ('ANT', False) for Ant, and ('ANT', True) for ant; True is sorted after False and so uppercased words are sorted before their lowercase equivalent.

See the Python sorting HOWTO for more information.

Alphabetical array sort - lowercase first

Test the first character and apply compare: