以周为单位对 NSDate 进行分组,并按从最新到最旧的顺序 [英] Group NSDate in weeks and order from newest to oldest

查看:79
本文介绍了以周为单位对 NSDate 进行分组,并按从最新到最旧的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 iPhone 开发一个应用程序,但我坚持到了这一点:

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...

此时我坚持如何按周对 NSDate 进行分组,当日期有更多年时,问题就开始了,因为例如结构中的第一个元素按最后一个元素分组.

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.

我该怎么办?谢谢

编辑:

这是我用来按周分组的代码:

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 是:

_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 和 32 是一年中的周数

31 and 32 are number of week in year

我认为这对我的目标来说是错误的...

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

以正确的方式对这个 NSDate 进行分组后,我必须有一个结构,允许我每周在 UITableView 中创建 n 个部分

推荐答案

您需要根据年份和一年中的第几周进行分组.您还需要避免对 52 个空数组进行硬编码.相反,使用数组字典,其中键代表年和周,值是数组.

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.

尝试这样的事情:

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;

这篇关于以周为单位对 NSDate 进行分组,并按从最新到最旧的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆