I'm trying to sort a dict by multiple keys. This is the dict I have:
standings = {1: {1: 1, 2: 0, 3: 1, 4: 0, 5: 0, 'player': 'Jack', 'points': 15},
2: {1: 1, 2: 0, 3: 2, 4: 2, 5: 0, 'player': 'Kate', 'points': 15},
3: {1: 0, 2: 0, 3: 1, 4: 0, 5: 0, 'player': 'Sawyer', 'points': 5}}
I want to sort it by, in this order: 'points'
, 1, 2, 3, 4, 5.
I could do this, I assume:
reversed(sorted(standings, key=lambda x: (standings[x]['points'],
standings[x][1],
standings[x][2],
standings[x][3],
standings[x][4],
standings[x][5])))
However, the 1, 2, 3, 4, 5 keys are dynamic (and could be 1, 2, 3, 4, 5, 6, 7, 8, 9, etc.)
So, somehow I want to make the sorting keys dynamic in sorted()
, except for 'points'
which will always be used.
The result I want is a reversed sorted list with the keys (which are player ids from the db) from the first dict. i.e. for the given example it will be[2, 1, 3]
.