2014-01-18 85 views
0

我有两个NSMutableArrayssearchResultssearchResultsDetails。我NSPredicate排序SearchResult所,我想searchResultsDetails是基于分类上的物体从searchResults移除了NSPredicate如何基于第一个用NSPredicate排序的数组对第二个数组进行排序?

例如,假设SearchResult所包含最初1,2,3,4,5和searchResultsDetails原来包含A,B,C,d,E。因此,与NSPredicate排序之后,SearchResult所包含1,2,5,我想searchResultsDetails包含A,B,E。我怎样才能做到这一点?

这是我使用到第一数组进行排序的代码。

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

self.searchResults = [self.lines filteredArrayUsingPredicate:resultPredicate]; 
+1

不是两个单独阵列的“行”和“细节”,你最好不过的所有信息整合在字典中的一个阵列。见http://stackoverflow.com/questions/19887505/wrong-cells-value-in-tableview-after-searching了类似的问题,一个可能的解决方案。 –

+0

Btw:你*过滤*(不*排序*)与谓词。 –

回答

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); 
相关问题