Search code examples
iosnsarray

Group NSArray results by date for tableview sections


I have the following NSArray, with the array i would like to group all the objects that containing the same date. I want to group these objects and then display them in a TableView, with that group the object with the same dates in their own sections in the TableView. In each section i want the events to be listed by the time in which the event will occur starting from earliest to latest time.

The data retrieved is from a Parse Database. (Not sure if this matters).

Below is the Array Results

DATA (

"<Sessions:QtJKSToEu9:(null)> {\n    date = \"2013-11-19 18:30:00 +0000\";\n    description = \"Seating assignment on name badge.\";\n    location = \"Center Stage\";\n    name = \"Dinner and CARE Awards Celebration\";\n}",

"<Sessions:btZ5eRZffl:(null)> {\n    date = \"2013-11-19 20:30:00 +0000\";\n    description = \"Brief meeting\";\n    location = \"Cedar Room\";\n    name = \"Store Manager Advisory Board Members\";\n}",

"<Sessions:HY4VtgBncd:(null)> {\n    date = \"2013-11-20 07:00:00 +0000\";\n    description = Breakfast;\n    location = Ballrooms;\n    name = Breakfast;\n}",

"<Sessions:6p6t2uL5Jf:(null)> {\n    date = \"2013-11-20 11:30:00 +0000\";\n    description = Lunch;\n    location = Ballrooms;\n    name = Lunch;\n}",

"<Sessions:TpEHi5Glsk:(null)> {\n    date = \"2013-11-20 17:00:00 +0000\";\n    description = \"Cocktails - Ballroom Pre-Function Area\";\n    location = Ballroom;\n    name = Cocktails;\n}",

"<Sessions:e9yEi6vFoE:(null)> {\n    date = \"2013-11-20 18:00:00 +0000\";\n    description = \"Welcome from Tony Kenney followed by dinner\";\n    location = Ballrooms;\n    name = Welcome;\n    speaker = \"Tony Kenney\";\n}",

"<Sessions:kTG4NemPX4:(null)> {\n    date = \"2013-11-20 19:30:00 +0000\";\n    description = \"We Are Speedway Awards Night\";\n    location = \"Center Stage\";\n    name = \"Awards Night\";\n}",

"<Sessions:L9I8BIVECe:(null)> {\n    date = \"2013-11-21 07:30:00 +0000\";\n    description = Breakfast;\n    location = \"Center Stage\";\n    name = Breakfast;\n}",

"<Sessions:WUGRHDpHyo:(null)> {\n    date = \"2013-11-21 12:00:00 +0000\";\n    description = Lunch;\n    location = \"Center Stage\";\n    name = Lunch;\n}",

"<Sessions:r61f5NZr66:(null)> {\n    date = \"2013-11-21 09:15:00 +0000\";\n    description = \"Marketing Breakouts \\U2013 Rotation\\no\\tStart times for rotations:  9:15, 9:50, 10:25, 11:00, 11:35\\no\\tPlease see your name tag for your Group #2 room assignment.\\no\\tThe speaker will direct you to your next session.\\n\";\n    location = Various;\n    name = \"Marketing Breakouts \";\n}",

"<Sessions:RXkIDNIYoQ:(null)> {\n    date = \"2013-11-20 13:30:00 +0000\";\n    description = \"o\\tPlease see your name tag for your Group #1 room assignment\";\n    location = Various;\n    name = \"Division Breakouts\";\n}",

"<Sessions:lLI2PxdCdx:(null)> {\n    date = \"2013-11-21 13:30:00 +0000\";\n    description = \"Closing Comments by Tony Kenny\";\n    location = \"Center Stage\";\n    name = \"Closing Comments\";\n    speaker = \"Tony Kenny\";\n}",

"<Sessions:KWz7Xen5a6:(null)> {\n    date = \"2013-11-20 13:00:00 +0000\";\n    description = \"Special Guest Speaker Gary Heminger\";\n    location = \"Center Stage\";\n    name = \"Special Guest Speaker\";\n    speaker = \"Gary Heminger\";\n}",

"<Sessions:AU1x3LlUrF:(null)> {\n    date = \"2013-11-21 08:30:00 +0000\";\n    description = \"Marketing Focus by Tom LeFevers\";\n    location = \"Center Stage\";\n    name = \"Marketing Focus\";\n    speaker = \"Tom LeFevers\";\n}",

"<Sessions:36TIRrD0s5:(null)> {\n    date = \"2013-11-20 08:00:00 +0000\";\n    description = \"Operations Focus by Glenn Plumby\";\n    location = \"Center Stage\";\n    name = \"Operations Focus\";\n    speaker = \"Glenn Plumby\";\n}",

"<Sessions:tTvppNFM7L:(null)> {\n    date = \"2013-11-19 17:00:00 +0000\";\n    description = \"Cocktails \\U2013 Center Stage pre-function area. NOTE: Group photographs scheduled for select field personnel\\nin the lobby bar. (Appointment time on name badge.)\";\n    location = \"Center Stage pre-function area \";\n    name = Cocktails;\n}"

)

How can i group the objects that contain the same date for example "2013-11-19" all into one section?

UPDATE With Artur Answers + Slight modification to fit my needs

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int section = 0;

dataSource = [NSMutableArray new];
[dataSource addObject:[NSMutableArray array]];

for (int i = 0; i < _events.count - 1; i++) {
    EventInfo *firstEvent = _events[i];
    EventInfo *secondEvent = _events[i+1];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MMMM, dd"];

    NSString *firstDateString = [df stringFromDate:firstEvent.details.date];
    NSString *secondDateString = [df stringFromDate:secondEvent.details.date];


    sectionArray = dataSource[section];

    if (![sectionArray containsObject:firstEvent]) {
        [sectionArray addObject:firstEvent];
    }
    if (![firstDateString compare:secondDateString] == NSOrderedSame) {
        NSMutableArray *newSection = [NSMutableArray array];
        [newSection addObject:secondEvent];
        [dataSource addObject:newSection];

        section++;
    }
   }
   [dataSource.lastObject addObject:_events.lastObject];

Solution

  • NSMutableArray *dataSource = [NSMutableArray new];
    
    // Sorting your array by date asc
    array = [[array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        YourObject *firstObject = obj1;
        YourObject *secondObject = obj2;
    
        return [secondObject.date compare:firstObject.date] == NSOrderedAscending;
    }] mutableCopy];
    
    
    int section = 0;
    [dataSource addObject:[NSMutableArray array]]; //Adding first section
    
    for (int i = 0; i < array.count - 1; i++) {
        YourObject *firstObject = array[i];
        YourObject *secondObject = array[i+1];
    
        NSDate *firstDate = [self dateWithoutTimePart:firstObject.date];
        NSDate *secondDate = [self dateWithoutTimePart:secondObject.date];
    
        NSMutableArray *sectionArray = dataSource[section];
    
        if (![sectionArray containsObject:firstObject]) {
            [sectionArray addObject:firstObject];
        }
        if (![firstDate compare:secondDate] == NSOrderedSame)
        {
            NSMutableArray *newSection = [NSMutableArray array];
            [newSection addObject:secondObject];
            [dataSource addObject:newSection];
    
            section++;
        }
    }
    
    // Adding last object to last section
    [dataSource.lastObject addObject:array.lastObject];
    

    This is a fast written code, so sorry for possible mistakes :)