Milo
Milo

Reputation: 5091

Sort array of dictionaries according to separate array ?

I have a bit of a strange problem. So I have an array of dictionaries say,

(
Dict 1:
"Name" = "B"
"Number" = "2"

Dict 2:
"Name" = "A"
"Number" = "1"

Dict 3:
"Name" = "D"
"Number" = "4"

Dict 4:
"Name" = "C"
"Number" = "3"

Dict 5
"Name" = "E"
"Number" = "5"
)

And say I have an array:

(
"1"
"4"
"2"
)

How can I get an array that contains all the dictionaries that their "Number" key match each object in that array and have it sorted according to the second array?

e.g. output would be

(
Dict 1:
"Name" = "A"
"Number" = "1"

Dict 2:
"Name" = "D"
"Number" = "4"

Dict 3:
"Name" = "B"
"Number" = "2"
)

I think I can use sortedArrayUsingDescriptors but I'm not sure how to specify multiple descriptors. Can the descriptors contain every object in the array? Any help or guidance is much appreciated.

Upvotes: 3

Views: 557

Answers (4)

A_G
A_G

Reputation: 69

for (NSNumber *aNumber in testArray) {
    for (NSDictionary *aDict in anArray) {
        if (aNumber.intValue == [(NSNumber *)[aDict valueForKey:@"Number"]intValue]) {
            [finalArray addObject:aDict];
        }
    }
}

Upvotes: 1

Milo
Milo

Reputation: 5091

So I figured it out. Basically I used nested for-loops.

Here is the code. It may take a long time depending on how many objects are in each. For me I had about 30 objects each so it took about 50ms:

NSMutableArray *finalArray = [NSMutableArray array];
for (NSString *keyName in keyArray) {
    NSString *fname;
    for (NSDictionary *dict in dictArray) {
        fname = [dict objectForKey:@"theKey"];
        if ([fname isEqualToString:keyName])
            [finalArray addObject:dict];
    }
}

Upvotes: 0

Rajath Shetty K
Rajath Shetty K

Reputation: 424

Here is the answer. here I used enumerator, which is faster

NSArray * dicArray = @[@{@"Name": @"B",@"Number":@"2"},
                       @{@"Name": @"A",@"Number":@"1"},
                       @{@"Name": @"D",@"Number":@"4"},
                       @{@"Name": @"C",@"Number":@"3"},
                       @{@"Name": @"E",@"Number":@"5"}];
NSArray * indArray = @[@(1),@(4),@(2)];

NSMutableArray * resultArray = [NSMutableArray arrayWithCapacity:indArray.count];
[indArray enumerateObjectsUsingBlock:^(NSNumber* SearchValue, NSUInteger idx, BOOL *stop) {

    //search dicArray for object, which match your condition (i.e Number value == obj value)
    NSInteger index = [dicArray indexOfObjectPassingTest:^BOOL(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
        return ([(NSString *)obj[@"Number"] integerValue] == SearchValue.integerValue)? YES : NO;
    }];

    resultArray[idx] = dicArray[index];
}];

NSLog(@"%@",resultArray);

Upvotes: 0

matt
matt

Reputation: 534893

This has nothing to do with sorting - especially since not every dictionary in the array is even involved. What you want to do is extract each dictionary from the array.

This would be a lot simpler if you didn't have an array of dictionaries at all. Instead, you should start with one big dictionary where the key is the number and the value is whatever (perhaps the whole dictionary, perhaps the name alone). Now you can just look up each number as the key into that dictionary and grab the name.

{ 1 => A, 2 => B, 3 => C ...}

Now just grab objectForKey:@1, objectForKey:@4, objectForKey:@2 and you're done.

Upvotes: 3

Related Questions