问题
I have array of dictionary that needs to be grouped according to PO which is part of the element and also get the total of quantityOrdered according to the same PO. The PO is dynamic it means it can be any value that needs to be filtered and compute the quantityOrderd accordingly.
Please help.
{
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;
},
Apology for the confusion or incomplete question but i need to create a new array of dictionary with similar like this :
{
PO = PO1;
TotalQuanityReceived=7;
LineItems=3;
},
{
PO = PO2;
TotalQuanityReceived=3;
LineItems=2;
},
{
PO = PO3;
TotalQuanityReceived=6;
LineItems=2;
},
i updated my example and make it easy to read.
回答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;
}
More pain will come with PO values not conforming to NSCopying
.
回答2:
You can do it the clever way with KVC or the stupid easy way. Let's do it the stupid easy way!
Make an empty NSMutableDictionary. Let's call it dict
.
Cycle through your array of dictionaries. For each dictionary:
Fetch its PO. Call that value
thisPO
.Fetch
dict[thisPO]
. Was it nil?a. Yes. Okay, so this particular PO has not yet been encountered. Set
dict[thisPO]
to this dictionary's quantity received (as an NSNumber).b. No. Turn that value into an integer, add this dictionary's quantity received, and set the total back into
dict[thisPO]
(as an NSNumber).
Done! The result is not quite what you asked for; the result looks like this:
{
PO1 = 100;
PO2 = 120;
...
}
But now, you see, the work of totalling is done and it is easy to transform that into an array of dictionaries if that is what you want.
回答3:
Not 100% sure if this is what you are saying or not, but to sort an array of dictionaries based on one of the elements it would look something like this.
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);
I wasn't really sure what PO was, so I just used an NSNumber as an example. Look at this page for an overview of how you would compare a customer object. http://nshipster.com/nssortdescriptor/.
Then you can loop through the array, now in the correct order and build your next NSDictionary.
来源:https://stackoverflow.com/questions/21504990/how-to-group-array-of-nsdictionary-according-to-the-value-inside-the-element