Open this example in the interactive library
Source code
rows = [('Ada', 8), ('Ben', 9), ('Amy', 8)]
print(sorted(rows, key=lambda row: (-row[1], row[0])))Sample output
[('Ben', 9), ('Ada', 8), ('Amy', 8)]
Sort records by two keys. A self-contained example using the Python standard library.
Open this example in the interactive library
rows = [('Ada', 8), ('Ben', 9), ('Amy', 8)]
print(sorted(rows, key=lambda row: (-row[1], row[0])))[('Ben', 9), ('Ada', 8), ('Amy', 8)]