sort list in alphabetical order (with numbers preceding lowercase letters preceding uppercase letters

146 Views Asked by At

I want to order string list strating from

l = ['AAAA.html', 'aaa.html', 'index.html', 'diem', '1zz.html']

I want to have this output

['1zz.html', 'aaa.html', 'diem', 'index.html', 'AAAA.html']

How to implement soluzion in python?

1

There are 1 best solutions below

0
njzk2 On

Usual sort in python is:

number, uppercase, lowercase.

You want

number, lowercase, uppercase

You just need to use, as sorting key, the case-swapped input value:

>>> l = ['AAAA.html', 'aaa.html', 'index.html', 'diem', '1zz.html']
>>> sorted(l, key=str.swapcase)
['1zz.html', 'aaa.html', 'diem', 'index.html', 'AAAA.html']