将数组元素分组到另一个数组上 [英] Group elements of array on another array

查看:66
本文介绍了将数组元素分组到另一个数组上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,每个对象都有一个日期.我想将具有相同日期的每个对象分组到另一个数组上.我的想法是:按对象的日期对数组进行排序,对于数组上的每个对象执行以下操作:如果索引 i 处的对象与索引 i + 1 处的对象日期相同,则将索引 i 处的对象添加到临时数组;否则将索引 i 处的对象添加到临时数组,将该数组添加到我的输出数组并从我的临时数组中删除所有对象.

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.

我的代码是这样的:

- (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];
}

为什么如果我执行 NSLog(@"items on grouped %d",[_grouped count]); 它总是返回 0?

Why if I do NSLog(@"items on grouped %d",[_grouped count]); it always return 0?

推荐答案

您可以使用带键路径的过滤器数组来实现它为您提供具有不同日期的所有对象:

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.

扩展:

如果要忽略需要创建开始日期和结束日期的时间并将谓词更改为:

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];

要从具有特定格式的字符串创建 NSDate,您可以使用 NSDateFormatter,有很多示例可以做到这一点.

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

这篇关于将数组元素分组到另一个数组上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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