Group NSDate in weeks and order from newest to oldest

断了今生、忘了曾经 提交于 2019-12-11 10:04:53

问题


i'm developing an app for iPhone and I'm stuck to this point:

i get this data from an object:

{
    date = "2014-08-04 11:21:26 +0000";
    idChallenger = "-1";
    index = "-1";
    time = "-1";
},
   {
    date = "2015-07-31 14:50:40 +0000";
    idChallenger = 43;
    index = "-1";
    time = "-1";
},
    {
    date = "2015-07-31 16:18:57 +0000";
    idChallenger = "-1";
    index = "-1";
    time = "-1";
},
    {
    date = "2015-07-31 16:19:29 +0000";
    idChallenger = "-1";
    index = "-1";
    time = "-1";
},
    {
    date = "2015-08-04 15:25:26 +0000";
    idChallenger = "-1";
    index = "-1";
    time = "-1";
}

Now i should get all date, group by weeks and sort from newest to oldest...

At this time I'm stuck to how group NSDate by weeks, the problem starts when there are more years in date, because for example first element in structure is grouped by the last element.

how can I do? Thanks

EDIT:

this is the code that I use to group by weeks:

NSMutableArray *weekArray = [NSMutableArray array];
for (int i = 0; i<52; i++) {

    [weekArray addObject:[NSMutableArray array]];
}


for (int i = 0; i<_array.count; i++) {

    NSDictionary *dict = [_array objectAtIndex:i];

    NSDate *date = [dict objectForKey:@"date"];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:date];

    NSMutableArray *innerArray = weekArray[[dateComponents weekOfYear] -1];
    [innerArray addObject:dict];
}

for (int i = 0; i<weekArray.count; i++) {

    if ([[weekArray objectAtIndex:i] count] > 0) {

        NSNumber *iNum = [NSNumber numberWithInt:i+1];

        [_mutableDictionary setObject:[weekArray objectAtIndex:i] forKey:iNum];
    }
}

_mutableDictionary is:

31 =     (
            {
        date = "2015-07-31 16:19:29 +0000";
        idChallenger = "-1";
        index = "-1";
        time = "-1";
    },
            {
        date = "2015-07-31 16:19:23 +0000";
        idChallenger = "-1";
        index = "-1";
        time = "-1";
    },
            {
        date = "2015-07-31 16:18:57 +0000";
        idChallenger = "-1";
        index = "-1";
        time = "-1";
    },
            {
        date = "2015-07-31 14:50:40 +0000";
        idChallenger = 43;
        index = "-1";
        time = "-1";
    }
);
32 =     (
            {
        date = "2015-08-04 15:25:26 +0000";
        idChallenger = "-1";
        index = "-1";
        time = "-1";
    },
            {
        date = "2014-08-04 11:22:40 +0000";
        idChallenger = 43;
        index = "-1";
        time = "-1";
    }
);

31 and 32 are number of week in year

I think that this is wrong for my goal...

After group this NSDate in the right way, i must have a structure that allow me to create n section in UITableView for each week


回答1:


You need to base the grouping on the year and the week of year. You also need to avoid hardcoding 52 empty arrays. Instead, use a dictionary of arrays where the key represents the year and week and the value is the array.

Try something like this:

NSMutableDictionary *weeksDictionary = [NSMutableDictionary dictionary];

NSCalendar *calendar = [NSCalendar currentCalendar];
for (NSDictionary *dict in _array) {
    NSDate *date = [dict objectForKey:@"date"];
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:date];
    NSInteger year = dateComponents.year;
    NSInteger week = dateComponents.weekOfYear;
    NSInteger index = year * 100 + week;
    NSNumber *key = @(index);
    NSMutableArray *weekArray = weeksDictionary[key];
    if (!weekArray) {
        weekArray = [NSMutableArray array];
        weeksDictionary[key] = weekArray;
    }
    [weekArray addObject:dict];
}

This gives you an array for each year/week that has a date.

To use this with the table view, sort the keys of the dictionary to get an array of year/week values in order. Then you can split each key to get the year and week:

NSNumber *key = sortedKeysArray[indexPath.section];
NSInteger value = key.integerValue;
NSInteger year = value / 100;
NSInteger week = value % 100;


来源:https://stackoverflow.com/questions/31836482/group-nsdate-in-weeks-and-order-from-newest-to-oldest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!