xkm
xkm

Reputation: 271

Ordering Query results by Date (String) for GAE

I have thousands of Entities stored in the GAE datastore where a field "create" is saved as a date String (standard format).

My question would be, is it still possible to do a Query like this:

Query q = new Query("Post")
                .addSort("created", SortDirection.DESCENDING);

Where "created" is actually a date String, stored like this: 2014-12-11T14:31:43 -08:00" and actually get a descending order. I've tried it but the result set is just as random and not even in order by date.

Is there a way to tweak a GAE Query to achieve this with this data?

Upvotes: 1

Views: 498

Answers (2)

tj-recess
tj-recess

Reputation: 1809

Even if GAE has indexed date as string so '2014-12-11T14:31:43 -08:00' comes after '2014-12-04T14:31:43 -08:00' (just for example) which means natural sorting order should follow.

Have you indexed 'created' field? If not, GAE is gonna ignore the sort order you gave in query.

Upvotes: 2

janscas
janscas

Reputation: 629

It won't work that way.

You need the property to be a Date type.

import java.util.Date;

Date created = new Date();
Post.setProperty("created", created);

Maybe you can do a task to update all your entities with the created string actually being cast as a date and save it.

Upvotes: 0

Related Questions