I have an array of custom objects which contain date and revenue. The object called Game
.
I've sorted the array of objects using compare
:
- (NSArray*)sortByDate:(NSArray*)objects
{
NSArray *sorted = [objects sortedArrayUsingComparator:^(id firstObject, id secondObject) {
Game *firstGameObject = (Game*)firstObject;
Game *secondGameObject = (Game*)secondObject;
return [(NSDate*)firstGameObject.date compare:(NSDate*)secondGameObject.date];
}];
return sorted;
}
I want to sort them by date and then set them into a UITableView
sectioned by the month and year (April 2013, October 2015 and so on...)
I've tried to sort the dates into a MMMM YYYY
format but they added in the wrong order inside the array.
- (NSArray*)getSectionsTitles:(NSArray*)objects
{
NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale currentLocale];
dateFormatter.timeZone = calendar.timeZone;
[dateFormatter setDateFormat:@"MMMM YYYY"];
for (Game *game in objects)
{
NSString *sectionHeading = [dateFormatter stringFromDate:game.date];
[mutableSet addObject:sectionHeading];
}
return mutableSet.allObjects;
}