Reputation: 107
Does anyone have any idea how to retrieve the last(with the newest date) entity in objectify? I know how to make a query but how to retrieve the one with the newest date?
List<Transaction> fetched2 = ofy.query(Transaction.class).filter("someproperty", somepropertyvalue).order("date").list();
I could try to bubble sort it but I´m sure there is an easier way. THX
Upvotes: 5
Views: 3542
Reputation: 534
if you want to perform sorting in objectify you should user order by method. For example if you have a table Sample and you want to apply sorting on it
Ascending sorting Sample sample = ofy.query(Sample .class).filter("propertyname", "propertyvalue).order("+date").list().first();
gets the first record in the
Descending sorting Sample sample = ofy.query(Sample .class).filter("propertyname", "propertyvalue).order("+date")
Upvotes: 0
Reputation: 80340
I don't know about your use case, but if you are tying to get "new" entities (ones added/updated after the last query) you might want to use Cursors. They are much more efficient as they only get new/updated entities: https://developers.google.com/appengine/docs/java/datastore/queries#Query_Cursors
Upvotes: 0
Reputation: 294
You just have to add a minus in front of "date" in your order :
List<Transaction> fetched2 = ofy.query(Transaction.class).filter("someproperty", somepropertyvalue).order("-date").list();
That should return you a list of Transaction with the newest one in first position.
Upvotes: 8