Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Query in Ascending using Parse

Currently even if I do orderByAscending it never does it by ascending. What is the issue 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 would be very random. I am assuming that the list is not being sorted while it is querying. I am unsure how to solve this problem though.

like image 437
user3281743 Avatar asked Feb 12 '16 19:02

user3281743


Video Answer


1 Answers

I use the NSSortDescriptor variant to do sorting with the Parse SDK and haven't had any issues with that (also filtering on multiple keys like you have is just fine).

This is how you would sort using a descriptor as opposed to a 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];
like image 89
Gavin Bunney Avatar answered Nov 15 '22 06:11

Gavin Bunney