← All tips

Python: sorting with key functions

pythonbasics

Use key= rather than custom comparisons.

items = ['aaa', 'b', 'cc']
print(sorted(items, key=len))