2014-01-18 89 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); 
相關問題