如何在ios中对包含字典的nsmutable数组进行排序

时间:2016-07-11 05:20:09

标签: ios objective-c sorting nsmutablearray

我有一个数组,并且有这样的数据。

(
    {
        avatar = "306_20160701_112934.jpg";
        email = "hardik@gmail.com";
        "full_name" = "hardik ";
        number = 977743;
        "user_id" = 306;
    },
    { 
        avatar = "profileImage_1466836096488.jpg";
        email = "robo@gmail.com";
        "full_name" = "Robot Codol";
        number = 903360;
        "user_id" = 324;
    },
    {
        avatar = "386_20160705_120501.jpg";
        email = "ravi@gmail.com";
        "full_name" = "apu ";
        number = 46576246876;
        "user_id" = 386;
     },
     {
        avatar = "profileImage_1467293381491.jpg";
        email = "testnew@gmail.com";
        "full_name" = "Robot Codol";
        number = 905660;
        "user_id" = 402;
     },
     {
        avatar = "474_20160704_073537.jpg";
        email = "demo@flikit.com";
        "full_name" = "hardik ";
        number = "+9777743";
        "user_id" = 474;
     }
)

现在我想按“full_name”的加入顺序对这些细节进行排序。你能帮忙吗。

我尝试过这段代码,但无法做到。

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"full_name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[arrSeggestedContact sortUsingDescriptors:sortDescriptors];

帮助谢谢。

2 个答案:

答案 0 :(得分:3)

试试这个

  NSSortDescriptor *sortDescriptor =[[[NSSortDescriptor alloc]
      initWithKey:@"full_name"
      ascending:YES
      selector:@selector(localizedCaseInsensitiveCompare:)]; // or try with `caseInsensitiveCompare`

答案 1 :(得分:2)

NSSortDescriptor *fullNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"full_name"
  ascending:YES
  selector:@selector(localizedStandardCompare:)];

NSLog(@"By Full name: %@", [people sortedArrayUsingDescriptors:@[fullNameSortDescriptor]]);