My NSArray inside of a PFObject needs to be sorted. The PFObject contains multiple items, including an array, which contains days of the week, and I need to know which date is repeated most often for each item inside the PFObject. Inside my cellForRowAtIndexPath (since I need to know it for each item in the TableView, I have this:
NSArray *yourArrayhere = object[@"DatesSuggested"];
if (yourArrayhere == NULL) {
//cell.text = @"No votes have been cast for this activity yet.";
}
else {
NSCountedSet *setOfObjects = [[NSCountedSet alloc] initWithArray:yourArrayhere];
//Declaration of objects
NSString *mostOccurringObject = @"";
NSUInteger highestCount = 0;
//Iterate in set to find highest count for a object
for (NSString *strObject in setOfObjects)
{
NSUInteger tempCount = [setOfObjects countForObject:strObject];
highestCount = tempCount;
mostOccurringObject = strObject;
NSLog(@"MOSTO%@", mostOccurringObject);
NSLog(@"MOSTC%lu", (unsigned long)highestCount);
}
NSString *allcombined = [[[[[[[@"Currently there are " stringByAppendingString:votesFor] stringByAppendingString:@" votes for and "] stringByAppendingString:votesAgainst] stringByAppendingString:@" votes against "] stringByAppendingString:object[@"Title"]] stringByAppendingString:@", with the most suggested date being "] stringByAppendingString:mostOccurringObject];
cell.detailTextLabel.text = allcombined;
NSString *toSave = [mostOccurringObject stringByAppendingString:[NSString stringWithFormat:@" %lu", (unsigned long)highestCount]];
NSLog(@"Highest %@", toSave);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:toSave forKey:cell.textLabel.text];
[defaults synchronize];
}
This works some of the time. One of the arrays has (Monday, Monday, Monday, Thursday) in it, and it shows Monday being most popular, with 3 votes for it. Another array is identical, but shows Thursday being most popular. I'm stumped on why it is behaving this way.