Skip to content

Instantly share code, notes, and snippets.

@zeayes
Created January 13, 2015 06:30
Show Gist options
  • Save zeayes/bd7187d8edaf28911da1 to your computer and use it in GitHub Desktop.
Save zeayes/bd7187d8edaf28911da1 to your computer and use it in GitHub Desktop.
objective c example
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@property NSNumber *age;
@end
@implementation Person
- (NSString *)description {
return [NSString stringWithFormat:@"%@ %@", self.lastName, self.firstName];
}
@end
int main(int argc, const char *argv[]) {
@autoreleasepool {
NSArray *firstNames = @[@"Alice", @"Bob", @"Charlie", @"Quentin"];
NSArray *lastNames = @[@"Smith", @"Jones", @"Smith", @"Alberts"];
NSArray *ages = @[@24, @27, @33, @31];
NSMutableArray *people = [NSMutableArray array];
[firstNames enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
Person *person = [[Person alloc] init];
person.firstName = [firstNames objectAtIndex:index];
person.lastName = [lastNames objectAtIndex:index];
person.age = [ages objectAtIndex:index];
[people addObject:person];
}];
NSSortDescriptor *firstNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"firstName"
ascending:YES
selector:@selector(localizedStandardCompare:)];
NSSortDescriptor *lastNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"lastName"
ascending:YES
selector:@selector(localizedStandardCompare:)];
NSSortDescriptor *ageSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age"
ascending:NO];
NSLog(@"By age: %@", [people sortedArrayUsingDescriptors:@[ageSortDescriptor]]);
NSLog(@"By first name: %@", [people sortedArrayUsingDescriptors:@[firstNameSortDescriptor]]);
NSLog(@"By last name, first name: %@", [people sortedArrayUsingDescriptors:@[lastNameSortDescriptor, firstNameSortDescriptor]]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment