E.g. I have:
JavaScript
x
2
1
x = ["Column a is...", "Column ab uses...", "Column b has..."]
2
then I need to get:
JavaScript
1
2
1
x = ["Column a is...", "Column b has...", "Column ab uses..."]
2
Advertisement
Answer
If the column identifier is guaranteed to be the second whitespace delimited token in each string then:
JavaScript
1
11
11
1
x = ["Column a is...", "Column ab uses...", "Column b has..."]
2
3
def conv(s):
4
n = 0
5
for c in reversed(s.split()[1].lower()):
6
n *= 26
7
n += ord(c) - ord('a')
8
return n
9
10
print(sorted(x, key=conv))
11
The conv() function takes the column identifiers and converts them into a value that can be used for natural sorting
Output:
JavaScript
1
2
1
['Column a is...', 'Column b has...', 'Column ab uses...']
2