2

I have a few objects in a Parse database, a few are as shown below. I would like to sort these objects by highScore, which is stored as a number. (

"<Score: 0x7fee53740700, objectId: zMjL3eNWpI, localId: (null)> {\n    Score = \"High Score: 60\";\n    TeamName = \"Team0\";\n    highScore = 60;\n}",

"<Score: 0x7fee534b5080, objectId: nysaJjYsth, localId: (null)> {\n    Score = \"High Score: 86\";\n    TeamName = Team1;\n    highScore = 86;\n}",

"<Score: 0x7fee535f6ad0, objectId: 7Hj8RP4wYD, localId: (null)> {\n    Score = \"High Score: 23\";\n    TeamName = Team2;\n    highScore = 23;\n}"

)

I have the following code which I loop over the objects and pull out the Number highScore for each object, but I am not sure how to continue. Is there a way I can return (NSComparisonResult)NSOrderedAscending? If anyone has any advice please let me know. Thanks.

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];//class name is Score
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {            
        for (int i = 0; i<=objects.count; i++){
        NSArray *array = [objects objectAtIndex:i];//Selects the first "object" from all the "objects"
        NSNumber *test= [objects objectAtIndex:i];
        array = [array valueForKey:@"highScore"];
        test = [test valueForKey:@"highScore"];               
         test1 = [test intValue];//test1 is the current database object highScore for the current object                        
        }     
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];

1 Answer 1

2

Parse.com offers a great iOS SDK that already gives you out of the box what you are looking for. When you make a PFQuery, Parse gives you the option to order the results the way you would like. I believe you should try this:

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query orderByAscending:@"highScore"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {

        for (PFObject *object in objects) {
            NSLog(@"High Score is %d", [object["highScore"]intValue]);
        }

    } else {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

}];
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! this line worked: [query orderByAscending:@"highScore"]; When I tried to query findObjectsInBackgroundWithBlock I got the following error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This query has an outstanding network connection. You have to wait until it's done.' I removed that and the Leader board is sorted though. Thanks Brandon!
You got it! Be sure to check out PFQueryTableViewController as well. Parse provides this class with their Parse.UI Framework that comes with their SDK. This is a custom GUI tool that assists you in querying data, persisting the data, and displaying your data in sorted UITableView. And if you think that's cool, check out PFQueryCollectionViewController!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.