How to execute a query in ascending order using analysis

Currently, even if I do orderByAscending, it never does it in ascending order. What is the problem that I do not see? I am using parse

PFQuery *foodList = [PFQuery queryWithClassName:@"Food"]; [foodList whereKey:@"date" greaterThanOrEqualTo:minimumDate]; [foodList whereKey:@"date" lessThan:maximumDate]; [foodList orderByAscending:@"expiration_date"]; [foodList findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { }]; 

Example

 food_name expiration_date Apple 1/2/15 Banana 1/2/15 Pear 1/3/15 Kiwi 1/1/15 

Output

The outputs will be very random. I assume the list is not sorted at the time of the request. I am not sure how to solve this problem.

+6
source share
2 answers

I use the NSSortDescriptor option for sorting with the Parse SDK and have no problems with this (also filtering by multiple keys, as you have everything in order).

This is how you would sort the handle, not the key:

 PFQuery *foodList = [PFQuery queryWithClassName:@"Food"]; [foodList whereKey:@"date" greaterThanOrEqualTo:minimumDate]; [foodList whereKey:@"date" lessThan:maximumDate]; NSSortDescriptor *orderBy = [NSSortDescriptor sortDescriptorWithKey:@"expiration_date" ascending:YES]; [foodList orderBySortDescriptor:orderBy]; 
+3
source

I found that Parse Query is pretty unreliable as soon as you start adding to multiple filters, and this might be a bug, but there seems to be no big reason for this. What I did when I have several filters is NSPredicate.

 NSPredicate *minPredicate = [NSPredicate predicateWithFormat:@"date >= %@", minimumDate]; NSPredicate *maxPredicate = [NSPredicate predicateWithFormat:@"date < %@", maximumDate]; NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[minPredicate,maxPredicate]]; PFQuery *foodList = [PFQuery queryWithClassName:@"Food" predicate:predicate]; [foodList orderByAscending:@"expiration_date"]; [foodList findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { }]; 

Another possibility is that your query filters the date, and then you order "expiration_date", and I'm not sure if there is a gap between the two. Make sure your item list is what you want.

+1
source

All Articles