views:

177

answers:

2

How can I order the results of Query using more than 1 property?

For example, I want to order my Query results by 'lastname' and order by 'firstname' in each group of 'lastname'.

+1  A: 

Just use two orders separated by comma, e.g.:

SELECT * FROM person ORDER BY lastname, firstname

See here for more details.

Aviad Ben Dov
but it's GqlQuery not Query
Oleksandr Bolotov
I can use GqlQuery instead, but I wonder if Query supports this kind of ordering in any way. I could not find any example on the API documentation.
Kei
Oh, I didn't get that from the question... Did you try `.order("lastname, firstname")`?
Aviad Ben Dov
That does not seem like a valid argument for order. I'll use GqlQuery and move on. Thanks for the help!
Kei
+2  A: 

Have you tried:

Person.all().order("lastname").order("firstname")

Oleksandr Bolotov
Yes, I did. However, order("firstname") breaks the grouping on "lastname".
Kei
Surely that's a bug, then. http://code.google.com/appengine/docs/python/datastore/queryclass.html#Query_order says "Adds an ordering for the results. Results are ordered starting with the first order added".
Steve Jessop
Works For Me. This is pretty basic functionality - you can definitely chain order clauses like this.
Nick Johnson