如何根据元素内部的值对nsdictionary数组进行分组

时间:2014-02-01 23:22:35

标签: ios objective-c filter nsmutablearray nsdictionary

我有一些字典需要根据PO进行分组,PO是元素的一部分,并且根据相同的PO获得quantityOrdered的总数。 PO是动态的,这意味着它可以是任何需要过滤的值,并相应地计算quantityOrderd。

请帮忙。

{
    PO = PO2;
    QuantityReceived = 1;
},
{
    PO = PO1;
    QuantityReceived = 3;
},
{

    PO = PO1;
    QuantityReceived = 3;
},
{
    PO = PO3;
    QuantityReceived = 2;
},
{
    PO = PO2;
    QuantityReceived = 2;
},
{
    PO = PO3;
    QuantityReceived = 4;
},
{

    PO = PO1;
    QuantityReceived = 1;
},

为混淆或不完整的问题道歉,但我需要创建一个新的字典数组,类似于:

{
   PO = PO1;
   TotalQuanityReceived=7;
   LineItems=3;
},
{
   PO = PO2;
   TotalQuanityReceived=3;
   LineItems=2;
},
{
   PO = PO3;
   TotalQuanityReceived=6;
   LineItems=2;
},

我更新了我的示例并使其易于阅读。

3 个答案:

答案 0 :(得分:1)

你可以用KVC或愚蠢的简单方法巧妙地做到这一点。让我们以愚蠢的方式做到这一点!

制作一个空的NSMutableDictionary。我们称之为dict

遍历您的词典数组。对于每个字典:

  1. 获取其PO。称该值为thisPO

  2. 抓取dict[thisPO]。它没有?

    一个。是。好的,所以还没有遇到这个特定的PO。将dict[thisPO]设置为此字典收到的数量(作为NSNumber)。

    湾不。将该值转换为整数,添加此字典的接收数量,并将总数设置为dict[thisPO](作为NSNumber)。

  3. 完成!结果并不是你所要求的;结果如下:

    {
        PO1 = 100;
        PO2 = 120;
        ...
    }
    

    但是现在,你知道,总计的工作已经完成,如果这就是你想要的,很容易将它转换成一个字典数组。

答案 1 :(得分:1)

- (NSArray *)whatever:(NSArray *)dictionaries
{
    NSMutableArray *results = [[NSMutableArray alloc] init];
    NSMutableDictionary *resultsByPO = [[NSMutableDictionary alloc] init];

    for (NSDictionary *dictionary in dictionaries) {
        id po = [dictionary objectForKey:@"PO"];
        NSMutableDictionary *result = [resultsByPO objectForKey:po];

        if (result == nil) {
            result = [[NSMutableDictionary alloc] init];
            [resultsByPO setObject:result forKey:po];
            [results addObject:result];
            [result setObject:po forKey:@"PO"];
        }

        double total = [[result objectForKey:@"TotalQuantityReceived"] doubleValue];
        total += [[dictionary objectForKey:@"QuantityOrdered"] doubleValue];

        int count = 1 + [[result objectForKey:@"Count"] intValue];

        [result setObject:@(total) forKey:@"TotalQuantityReceived"];
        [result setObject:@(count) forKey:@"Count"];
    }
    return results;
}

PO值不符合NSCopying会带来更多痛苦。

答案 2 :(得分:0)

不是100%确定这是否是你所说的,而是基于其中一个元素对字典数组进行排序,它看起来像这样。

NSDictionary *d1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                       [NSNumber numberWithDouble:100],@"PO",
                       [NSNumber numberWithDouble:0], @"Category",
                       nil];

NSDictionary *d2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                    [NSNumber numberWithDouble:50],@"PO",
                    [NSNumber numberWithDouble:90], @"Category",
                    nil];

NSArray *unsorted = @[d1, d2];

NSArray *sortedArray;
sortedArray = [unsorted sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSDictionary *first = (NSDictionary*)a;
    NSDictionary *second = (NSDictionary*)b;

    NSNumber *firstPO = [first objectForKey:@"PO"];
    NSNumber *secondPO = [second objectForKey:@"PO"];

    return [firstPO  compare:secondPO];
}];

NSLog(@"unsorted = %@", unsorted);

NSLog(@"sorted = %@", sortedArray);

我不太确定PO是什么,所以我只是以NSNumber为例。请查看此页面,了解如何比较客户对象。 http://nshipster.com/nssortdescriptor/

然后你可以循环遍历数组,现在按正确的顺序构建你的下一个NSDictionary。