2014-10-10 90 views
0

我想这取决于在阵列self.contacts使用排序子数组键值对

守则FIRST_NAME用来阵列

-(void)Sort 
{ 
    NSSortDescriptor *sdName = [[NSSortDescriptor alloc] initWithKey:@"first_name" ascending:YES]; 
    self.contacts = [NSMutableArray arrayWithArray:[self.contacts sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sdName, nil]]]; 
} 
排序排序我的主阵列的主要的NSArray

迈德特

self.contacts = (
     { 
      contact = { 
       "first_name" = "Test R"; 
      }; 

      "last_met" = (
       { 
        "last_met_date" = "2014-10-09"; 
       } 
      ); 
     }, 
     { 
      contact = { 
       "first_name" = "Test K"; 
      }; 

      "last_met" = (
       { 
        "last_met_date" = "2014-10-09"; 

       } 
      ); 
     } 
    ) 
+0

请添加更多信息。你的问题非常模糊。 – TMob 2014-10-10 08:55:43

回答

1

尝试用这种描述:

descriptor = [[NSSortDescriptor alloc] initWithKey:@"contact.first_name" 
             ascending:YES]; 

参数key实际上是一个关键路径,并NSDictionary是KV兼容。你可以写这样的东西:[contacts[0] valueForKeyPath:@"contact.first_name"]

1

看看你的代码,它看起来像你想排序联系人数组,这是一个字典数组,我在这里提供的解决方案是这种情况。

你可以这样说:

NSArray *sortedContactsArray = [self.contacts sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
    NSString *first_name1 = [obj1 valueForKeyPath:@"contact.first_name"]; 
    NSString *first_name2 = [obj2 valueForKeyPath:@"contact.first_name"]; 

    return [first_name1 compare:first_name2]; 
}]; 

注意,这可能是不安全的,优化的,但这些都只是做到这一点的方法之一。