Reputation: 141
I have a list of flights, which are to be sorted on departure date first, and then to be sorted on the total flight duration, keeping their order according to the date.
output should be:
I tried:
sorted(flights, key=methodcaller('date','flighttime'))
but methodcaller only takes 1 argument. I tried also groupby first and then sort on flighttime, but then the list gets sorted to flighttime only.
thank you
Upvotes: 2
Views: 133
Reputation: 140256
methodcaller
cannot call more than 1 method, the other arguments are parameters:
f = methodcaller('name', 'foo', bar=1), the call f(b) returns b.name('foo', bar=1)
So it can be done using methodcaller
but in a more complex way probably involving lambda
like (untested) lambda x : methodcaller('name')(x),methodcaller('flighttime')(x)
So I would use a simple lambda
instead (where x
is a Flight
object):
sorted(flights, key=lambda x : (x.date(),x.flighttime()))
Upvotes: 2