Case: I want to sort a list of objects on two attributes. The first one is an integer which represents the points scored. The second attribute is a datetime.timedelta object.
My goal: I want to sort the list first on the score (highest on top), and after on timedelta (shortest/smallest first). The end result should be a list ordered on the score, and when there is an even score the time should decide who wins by the shortest time.
I have the following code:
def show_result_full(self):
result = self.sessions.all()
result.sort(key=lambda x: (x.score, x.session_time), reverse=True)
Steps in code:
- Get all sessions
- Sort the sessions as described above
This does not work because now it's reversed on both attributes. Looking forward to your suggestions because I am really stuck on this.