views:

83

answers:

1

Trying to sort by date with lambda I can't understand which lambda my error message is referring to. The message is

<lambda>() takes exactly 1 argument (2 given)

The 2 instructions are

a = A.proximity_fetch(A.all().filter("modified >", timeline).filter("published =", True).filter("modified <=", bookmark ).order("-modified") ,db.GeoPt(lat, lon),max_results=40, max_distance=m)
a = sorted(a, lambda x: x.modified, reverse=True)

Thanks for the help!

+7  A: 

Use

a = sorted(a, key=lambda x: x.modified, reverse=True)
#             ^^^^

On Python 2.x, the sorted function takes its arguments in this order:

sorted(iterable, cmp=None, key=None, reverse=False)

so without the key=, the function you pass in will be considered a cmp function which takes 2 arguments.

KennyTM
+1. Makes far more sense.
Manoj Govindan
Perfect! This solved a major problem I had for long. It works and I can use it. I also will try learn and understand what I do.
LarsOn
You have good chance to learn to appreciate keyword parameter passing from this experience.
Tony Veijalainen