ksskr ksskr - 1 year ago 58
Java Question

sorting by time column in data store using google app engine

I am using google app engine to save comments on my website. I would want to sort these by a particular column. I save the time a comment is posted in this column. So the datastore looks like this

id message time
name=seth good luck Jul 30, 2014 2:04:06 PM
name=henry good luck Jul 30, 2014 7:03:31 PM

Now i would like to sort the entities by time.
I am using Java as the language for this.

This is how i am writing the query to give me sorted results by time (comment is the entity name)

Query q = new Query("comment").addSort("time", SortDirection.DESCENDING);
PreparedQuery pq = datastore.prepare(q);

This surely does not work. I went through the docs but could not figure out a way to achieve this. Any help will be greatly appreciated.

Answer Source

Most likely, you did not make "time" an indexed property. From the documentation:

A query can't find property values that aren't indexed, nor can it sort on such properties. See the Datastore Indexes page for a detailed discussion of unindexed properties.


You store it as a String. It will sort it as a String, not by time. You should store it as a Long value - it will save you a lot of space and the sorting will be correct.