hardik vyas hardik vyas - 4 months ago 8
Objective-C Question

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 = "hardik@gmail.com";
"full_name" = "hardik ";
number = 9725277743;
"user_id" = 306;
},{
avatar = "profileImage_1466836096488.jpg";
email = "robo@gmail.com";
"full_name" = "Robot Codol";
number = 9033195660;
"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 = 9033195660;
"user_id" = 402;
},{
avatar = "474_20160704_073537.jpg";
email = "demo@flikit.com";
"full_name" = "hardik ";
number = "+919725277743";
"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.

Answer

try this

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