0

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?

1
  • "I want to group on another array each object with the same date." Does using NSMutableDictionary (key=Date, value=Array of same date Objects) will work in your case ? Commented Jan 24, 2014 at 16:34

2 Answers 2

1

You can do it with filter array with key path it gives you all object with distinct date:

NSArray *distinct = [_storeManager.activities valueForKeyPath:@"@distinctUnionOfObjects.date"]

Now you have to create loop to enumerate all object and filter it with predicate for date:

for(NSString *dateAsString in distinct)
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", dateAsString];
    NSArray *arrayForTheSameDay = [_storeManager.activities  filteredArrayUsingPredicate: predicate];
}

You just need to amend this code to match your properties.

Extended:

If you want to ignore the time you need to create start date and end date and change the predicate to:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];

To create NSDate from string with specific format you can use NSDateFormatter, there are plenty examples how to do that.

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

4 Comments

Thanks, is it possible to filter from date only year, month, date? Because the date object have the hours and seconds so they are all differents.
@mattyahtzee the quickest way to achieve what you want is create nsdateformatter when you add object to the array and format dates to contain just year month and day.
from which array? Unfortunately, I can't modify the array _storeManager.activities because hours and minutes are useful on another part of application.
Less coding and very technical.
0

Suggestion: Use an NSMutableDictionary with the dates as keys and NSMutableArrays as values.

You don't have to sort the initial array. Just walk through it, and for each date you encounter, see if a key of that date is already in the dictionary. If not, create an array and add it to the dictionary with the date as the key. If the date is found, get the array corresponding to the key and add the new object to it.

At the end you should have an NSMutableDictionary with all the various dates as keys, and all the corresponding objects as values of those keys. Think of it as a frequency analysis.

As for your question on the grouped count being zero, sorry, I didn't follow the code.

1 Comment

Unfortunately I should use an NSMutableArray.

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.