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:
var myArray = ['d', 'a', 'k', 'v', 'z', 'A', 't', 'G', 'V'];
myArray.sort(function (a, b) { if (a[0] === a[0].toLocaleLowerCase() && b[0] === b[0].toLocaleLowerCase() || a[0] === a[0].toLocaleUpperCase() && b[0] === b[0].toLocaleUpperCase()) { return a.localeCompare(b); } if (a[0] === a[0].toLocaleLowerCase()) { return -1; } return 1;});
document.write('<pre>' + JSON.stringify(myArray, 0, 4) + '</pre>');
Related Topics
How to Remove Cached Credentials from Git
32-Bit Absolute Addresses No Longer Allowed in X86-64 Linux
What Does Set -E Mean in a Bash Script
Can Windows Containers Be Hosted on Linux
How to Run Script as Another User Without Password
Linking Against an Old Version of Libc to Provide Greater Application Coverage
How to Get Cmake to Find My Alternative Boost Installation
Automating Telnet Session Using Bash Scripts
Multiple Glibc Libraries on a Single Host
How to Set the Environmental Variable Ld_Library_Path in Linux
Difference Between Clock_Realtime and Clock_Monotonic
How to Get the Total Cpu Usage of an Application from /Proc/Pid/Stat
How to Ensure Only One Instance of a Bash Script Is Running
Error When Using Git Credential Helper With Gnome-Keyring as Sudo
What's the Difference Between "Statically Linked" and "Not a Dynamic Executable" from Linux Ldd