I have an array of object and each object have a date. I want to group on another array each object with the same date. My idea is this: sort the array by date of objects, for each object on array do this: if object at index i has the same date of object at index i + 1, add the object at index i to a temporary array; else add object at index i to a temporary array, add the array to my output array and remove all objects from my temporary array.
My code is this:
- (void) group{
NSMutableArray *aux = [[NSMutableArray alloc] init];
MyObject *currentObject;
MyObject *nextObject;
// Sort array
NSArray *sortedArray;
sortedArray = [_storeManager.activities sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSDate *first = [(MyObject *)a date];
NSDate *second = [(MyObject *)b date];
return [first compare:second];
}];
// Grouping array
if ([sortedArray count] > 0) {
for (int i = 0; i < [sortedArray count] -1 ; i++) {
currentObject = sortedArray[i];
nextObject = sortedArray[i+1];
int currentDayObject = [currentObject getDayOfDate];
int currentMonthObject = [currentObject getMonthOfDate];
int currentYearObject = [currentObject getYearOfDate];
int nextDayObject = [nextObject getDayOfDate];
int nextMonthObject = [nextObject getMonthOfDate];
int nextYearObject = [nextObject getYearOfDate];
if ((currentDayObject == nextDayObject) && (currentMonthObject == nextMonthObject) && (currentYearObject == nextYearObject)) {
[aux addObject:currentObject];
} else {
[aux addObject:currentObject];
[_grouped addObject:aux];
[aux removeAllObjects];
}
}
[_grouped addObject:aux];
}
Why if I do NSLog(@"items on grouped %d",[_grouped count]);
it always return 0?