0

i have a NSMutableArray "arrayCompaniesData" passing an NSMutableDictionary in it like [arrayCompaniesData addObject:mutableDic], in mutableDic i have Keys emp_id, name and description i want to sort this mutableArray by key 'name' how i can do this???? i did use NSSortDescriptor but that don't work.......

 NSArray *convertedArray_1 = [[NSArray alloc]initWithArray:arrayCompaniesData];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES];
    NSArray *sortArray = [NSArray arrayWithObject:sort];
    NSArray *srt = [convertedArray_1 sortedArrayUsingDescriptors:sortArray];
Anu
  • 89
  • 5
  • 1
    Why is your key: @"key" ? That isn't what you want to search by... Change it to @"name" – Wain Oct 28 '13 at 12:01

1 Answers1

0

Easiest way is probably blocks

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSDictionary *first = (NSDictionary*)a;
    NSDictionary *second = (NSDictionary*)b;
    return [first[@"name"] compare:second[@"name"]];
}];
jbat100
  • 16,757
  • 4
  • 45
  • 70