Tuesday, 17 September 2013

Sorting a PFQuery by counting objects in array

Sorting a PFQuery by counting objects in array

What I am doing is in my Parse query, I check to see which posts have been
check marked (liked) in the last 7 days. I then remove the dates of those
that haven't. This is working fine. Now my problem is that I need to sort
them in my UITableView in descending order of how many dates are in the
array but my sortedArrayUsingComparator isn't working. Im new to Objective
C so its probably something easy but idk. Here's my code
- (PFQuery *)queryForParse {
PFQuery *query1 = [PFQuery queryWithClassName:self.parseClassName];
//[query1 selectKeys: [NSArray arrayWithContentsOfFile:[ NSString
@"checkMark"]]];
[query1 whereKey:@"location" nearGeoPoint:[PFGeoPoint
geoPointWithLatitude:[LocationController
sharedSingleton].locationManager.location.coordinate.latitude
longitude:[LocationController
sharedSingleton].locationManager.location.coordinate.longitude]
withinMiles:50];
[query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError
*error) {
if (!error){
placesArray = [[NSMutableArray alloc] initWithArray:objects];
NSMutableArray *toDelete = [NSMutableArray array];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
for (PFObject *tempObject in placesArray){
checkDates = [[NSMutableArray alloc] initWithArray:[tempObject
objectForKey:@"checkMarkDates"]];
for (NSString *dateString in checkDates) {
NSDate *date = [dateFormat dateFromString: dateString];
if ([date timeIntervalSinceNow] > -604800){
[toDelete addObject:dateString];
}
}
[checkDates removeObjectsInArray:toDelete];
[tempObject removeObjectsInArray:checkDates
forKey:@"checkMarkDates"];
sortedArray = [placesArray
sortedArrayUsingComparator:^NSComparisonResult(id obj1, id
obj2) {
NSUInteger *first = [[obj1 objectForKey:@"checkMarkDates"]
count];
NSNumber *f = [[NSNumber alloc] initWithInteger:first];
NSUInteger *second = [[obj2
objectForKey:@"checkMarkDates"] count];
NSNumber *s = [[NSNumber alloc] initWithInteger:second];
return [f compare:s];
}];
}
NSLog(@"%@", sortedArray);
}
[placesTable reloadData];
}];
return query1;
}

No comments:

Post a Comment