LIST = ['aaa', 'aa', 'ba', 'b', 'AA', 'zz', 'c']
print (sorted(LIST, key=lambda s: s.casefold()))
print (sorted(LIST, key=lambda s: len(s)))
combine both into one Lambda - expected result:
['b', 'c', 'aa', 'AA', 'ba', 'zz', 'aaa']
In your key
parameter you can sort first by len
, then if those are equal it will fall back to sort by str.casefold
semantics.
>>> sorted(LIST, key=lambda s: (len(s), s.casefold()))
['b', 'c', 'aa', 'AA', 'ba', 'zz', 'aaa']