0

I have two NSMutableArrays: searchResults and searchResultsDetails. I sort searchResults with NSPredicate, and I want searchResultsDetails to be sorted based on which objects were removed from searchResults with the NSPredicate.

For example, let's say searchResults originally contains 1,2,3,4,5 and searchResultsDetails originally contains A,B,C,D,E. So after sorting it with NSPredicate, searchResults contains 1,2,5, and I want searchResultsDetails to contain A,B,E. How can I accomplish this?

This is the code I am using to sort the first array.

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"self CONTAINS %@",
                                searchText];

self.searchResults = [self.lines filteredArrayUsingPredicate:resultPredicate];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    Instead of two separate arrays for the "lines" and the "details", you better but all information together in one array of dictionaries. See http://stackoverflow.com/questions/19887505/wrong-cells-value-in-tableview-after-searching for a similar problem and a possible solution. – Martin R Jan 18 '14 at 08:43
  • Btw: You *filter* (not *sort*) with a predicate. – Martin R Jan 18 '14 at 08:44

1 Answers1

0
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects:@"ashu",@"toremove",@"ashuabc",@"toremove",@"ashu12345", nil];
NSMutableArray *descriptionArray = [[NSMutableArray alloc] initWithObjects:@"description for ashu",@"description for non ashu",@"description for ashuabc",@"description for nopnashu",@"description for ashu12345", nil];

NSMutableArray *removedIndexArray = [[NSMutableArray alloc] init];
NSMutableArray *sortedArray = [[NSMutableArray alloc] init];

for (int i = 0; i < [firstArray count]; i++) {
    NSString *tempStr = [firstArray objectAtIndex:i];

    if ([tempStr rangeOfString:@"ashu"].location == NSNotFound) {
        [removedIndexArray addObject:[NSString stringWithFormat:@"%d",i]];
    } else {
        [sortedArray addObject:tempStr];
    }
}

for (int j = 0; j < [removedIndexArray count]; j++) {
    [descriptionArray removeObjectAtIndex:[[removedIndexArray objectAtIndex:j] integerValue]];
}

NSLog(@"%@",descriptionArray);
NSLog(@"Sorted array is :: %@",sortedArray);
Ashutosh
  • 2,215
  • 14
  • 27