1

For example: I have two NSMutableArray. One @[1,2,3,4,5,6,7]. Other have objects like

@[
   @{@idObjectToSearch":1, @"name":@"aaaaa", @"surname": @"bbbbb"}, @{@idObjectToSearch":2, @"name":@"aaaaa", @"surname": @"ccccc"},
    ...
   @{@idObjectToSearch":100, @"name":@"aaaaa", @"surname": @"cccdcd"}
];

So how I could extract needed objects from second array more effective way?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Viktorianec
  • 361
  • 6
  • 22
  • 1
    What is the link between this two arrays, based on what you want to extract object from second array ? – Rajat Nov 25 '16 at 10:20
  • @Rajat I needed all objects by "idObjectToSearch" – Viktorianec Nov 25 '16 at 10:22
  • Unless you want to load the objects from the second array into a dictionary keyed by the `idObjectToSearch` value (and this is what i would do) any technique you use will be a linear search of the array – Paulw11 Nov 25 '16 at 10:30

2 Answers2

2

You need to use NSPredicate with your second array.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idObjectToSearch IN %@", firstArray];
//In above predicate instead of passing `1` you need to pass object from first array that you want.

NSArray *filterArray = [secondArray filteredArrayUsingPredicate:predicate];

//Now access Array objects
if (filterArray.count > 0) {
     NSLog(@"%@",filterArray);
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • It's return only one object, but I need all 7, as in example. So I could to form a long predicate with OR, but I didn't think that this is good way for my case. – Viktorianec Nov 25 '16 at 10:35
  • @Viktorianec then you need to use `IN` check the edited answer – Nirav D Nov 25 '16 at 10:37
1

You can do it like this

NSMutableArray * arrSorted = [NSMutableArray new];
for(int i=0;i<arr.count;i++) {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"idObjectToSearch == %@", firstArray[i]];
    NSUInteger index = [secondArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
        return [predicate evaluateWithObject:obj];
    }];
    if (index != NSNotFound) {
        [arrSorted addObject:[arrM objectAtIndex:index]];
    }
}

arrSorted will contain your sorted data

Rajat
  • 10,977
  • 3
  • 38
  • 55