-3
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']
azro
  • 53,056
  • 7
  • 34
  • 70
Adam
  • 85
  • 7

1 Answers1

1

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']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218