Skip to content

Commit

Permalink
Merge pull request #1 from Tagtoo/feature-order
Browse files Browse the repository at this point in the history
add order by function
  • Loading branch information
lucemia committed Aug 26, 2014
2 parents 90ac07d + 4a772c7 commit 14019fc
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,30 @@ def with_cursor(self, start_cursor, end_cursor=None):
if end_cursor:
self._pb.end_cursor = base64.b64decode(end_cursor)

def order(self, *properties):
"""Adds a sort order to the query. If more than one sort order is added,
they will be applied in the order specified.
:type properties: string
:param properties: String giving the name of the property on which to sort,
optionally preceded by a hyphen (-) to specify descending order.
Omitting the hyphen specifies ascending order by default.
:rtype: :class:`Query`
:returns: A Query order by properties.
"""
clone = self._clone()

for p in properties:
property_order = clone._pb.order.add()

if p.startswith('-'):
property_order.property.name = p[1:]
property_order.direct = property_order.DESCENDING
else:
property_order.property.name = p
property_order.direction = property_order.ASCENDING

return clone


0 comments on commit 14019fc

Please sign in to comment.