Hardik Vyas
Hardik Vyas

Reputation: 2253

How to sort nsmutable array which containing dictionary in ios

I have one array and in that having data like this.

(
    {
        avatar = "306_20160701_112934.jpg";
        email = "[email protected]";
        "full_name" = "hardik ";
        number = 977743;
        "user_id" = 306;
    },
    { 
        avatar = "profileImage_1466836096488.jpg";
        email = "[email protected]";
        "full_name" = "Robot Codol";
        number = 903360;
        "user_id" = 324;
    },
    {
        avatar = "386_20160705_120501.jpg";
        email = "[email protected]";
        "full_name" = "apu ";
        number = 46576246876;
        "user_id" = 386;
     },
     {
        avatar = "profileImage_1467293381491.jpg";
        email = "[email protected]";
        "full_name" = "Robot Codol";
        number = 905660;
        "user_id" = 402;
     },
     {
        avatar = "474_20160704_073537.jpg";
        email = "[email protected]";
        "full_name" = "hardik ";
        number = "+9777743";
        "user_id" = 474;
     }
)

Now I Want to sort this details in accending order by "full_name" Can you please help in this.

I have tried this code but cant do it.

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

Help in this thank you.

Upvotes: 1

Views: 79

Answers (2)

Saurabh Jain
Saurabh Jain

Reputation: 1698

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

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

Upvotes: 2

Anbu.Karthik
Anbu.Karthik

Reputation: 82766

try this

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

Upvotes: 3

Related Questions