For a mutable array of dictionaries:
NSMutableArray *aMutableArray = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"A", @"name",
[NSNumber numberWithInt:6], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"C", @"name",
[NSNumber numberWithInt:2], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"B", @"name",
[NSNumber numberWithInt:4], @"value", nil], nil];
In this case case here's how you sort it by value:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
Otherwise:
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES comparator:^(id left, id right) {
NSMutableArray *aMutableArray = [NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"A", @"name",
[NSNumber numberWithInt:6], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"C", @"name",
[NSNumber numberWithInt:2], @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:
@"B", @"name",
[NSNumber numberWithInt:4], @"value", nil], nil];
In this case case here's how you sort it by value:
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES];
[aMutableArray sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
Otherwise:
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES comparator:^(id left, id right) {
float v1 = [left floatValue];
float v2 = [right floatValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}];
[data sortUsingDescriptors:[NSArray arrayWithObject:lastNameDescriptor]];