-2

I am getting array of dictionary and I want to sort that data alphabetically to show indexed tableview functionality.how can i sort array with dictionary to implement indexed tableview.to sort array here is my code:

NSArray *array1 = [NSArray arrayWithArray:resultArray];
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES selector:@selector(caseSensitive)] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
sortedArray = [array1 sortedArrayUsingDescriptors:sortDescriptors];

I am trying with code but, when i am displaying data in tableview its getting mismatch.Please suggest me correct way. Thanks.

Dev
  • 1
  • 2

4 Answers4

0

please use this code:

NSArray *array1 = [NSArray arrayWithArray:resultArray];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
array1 = [array1 sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
Abhinandan Pratap
  • 2,142
  • 1
  • 18
  • 39
0

Try this

 NSArray *arr = @[@{@"first_name":@"B"},@{@"first_name":@"A"}];
 NSSortDescriptor *desc = [NSSortDescriptor sortDescriptorWithKey:@"first_name" ascending:YES];
arr =  [arr sortedArrayUsingDescriptors:@[desc]];
Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
0

We Got The Solution By Using The Method Follows

[self.jsonData sortUsingDescriptors: [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:"fullname" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:"id" ascending:NO], nil]];

Where:-

jsonData - MutableArray Which holds the Parsed JSON Data.

fullname - the data we want to sort.

id - An unique data which comes with the inner dictionary.

Ref: how to sort array of dictionary alphabetically in iOS 9

Community
  • 1
  • 1
Moin Shirazi
  • 4,372
  • 2
  • 26
  • 38
0

try this

let sortedKeys = (self.dictBrands.allKeys as! [String]).sort(<) self.arrAllBrandsTitle = NSMutableArray(array: sortedKeys)

Anny
  • 509
  • 1
  • 5
  • 14