2013-03-20 10 views
0

我有三個陣列像下面將在NSDictionary的陣列和責令

names      birthdate    remanning 

"Abhi Shah",     "01/14",     300 
"Akash Parikh",    "12/09/1989",    264 
"Anand Kapadiya",   "12/01",     256 
"Annabella Faith Perez",  "03/02",     347 
"Aysu Can",     "04/14/1992",    25 
"Chirag Pandya"    "10/07/1987"    201 

PLZ這將是很大的幫助,如果你給我如何在NSDictionary中添加這三個陣列和代碼,然後訂貨後(升序)整個字典按照「重新編排」排列

注意在Dic中一切都應該改變。不僅重新陣列。姓名和出生日期應以同樣的方式remaning天得到改變都得到改變

非常感謝你提前

+0

你想根據剩餘日期來分類嗎? – 08442 2013-03-20 07:19:09

+1

你在http://stackoverflow.com/questions/15517015/how-to-change-order-of-nsmutable-array-in-same-way-another-mutable-arrays-get-ch中添加相同的問題 – NANNAV 2013-03-20 07:31:41

回答

1

你必須採取在字典或任何每個記錄(姓名,出生日期,remanning)結構體。該字典的數組應該被創建。按照您的要求對數組進行排序可以使用任何排序機制。

-(void)sort 
{ 
    //This is the array of dictionaries, where each dictionary holds a record 
    NSMutableArray * array; 
    //allocate the memory to the mutable array and add the records to the arrat 

    // I have used simple bubble sort you can use any other algorithm that suites you 
    //bubble sort 
    // 
    for(int i = 0; i < [array count]; i++) 
    { 
     for(int j = i+1; j < [array count]; j++) 
     { 
      NSDictionary *recordOne = [array objectAtIndex:i]; 
      NSDictionary *recordTwo = [array objectAtIndex:j]; 

      if([[recordOne valueForKey:@"remaining"] integerValue] > [[recordTwo valueForKey:@"remaining"] integerValue]) 
      { 
       [array xchangeObjectAtIndex:i withObjectAtIndex:j]; 
      } 
     } 
    } 

    //Here you get the sorted array 
} 

希望這會有所幫助。

3

我勸你變化設計項目的創建一個模型的一樣其性質:

@interface YourModel : NSObject 
    @property (strong) NSString *name; 
    @property (strong) NDDate *birthDate; 
    @property NSInteger remaining; 
@end 

,然後在類中創建一個NSMutableArray,並繼續進行添加。

這將使您的工作更容易,因爲搜索,排序,過濾,而不是處理3個並行陣列。

1

如果您使用Anoop提出的設計,採用塊排序的代碼將與此類似:

NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { 
    NSInteger first = [(YourModel*)a remaining]; 
    NSInteger second = [(YourModel*)b remaining]; 
    return [first compare:second]; 
}];