0

I would like to sort a list of namedTuple base on 2 parameters. For example, my data has a list of tuples containing (ExampleTuples):

Example(key=0,time=15)
Example(key=1,time=50)
Example(key=2,time=15)
Example(key=4,time=3)

I want to sort them with descending order base on time, but also sort them with ascending order of key. Basically I want the sort to be:

Example(key=1,time=50)
Example(key=0,time=15)
Example(key=2,time=15)
Example(key=4,time=3)

If I use:

SortedTuple = list(reversed(sorted(ExampleTuples, key=lambda x: (x.time,x.key))))

I get:

Example(key=1,time=50)
Example(key=2,time=15)
Example(key=0,time=15)
Example(key=4,time=3)

Whats the best way to get what I want?

user1179317
  • 2,693
  • 3
  • 34
  • 62

0 Answers0