Create a gist now

Instantly share code, notes, and snippets.

@edenwaith /sorting.m
Last active Aug 29, 2015

Objective-C Searching and Sorting with NSPredicate
/*
* File: sorting.m
* Author: Chad Armstrong
* Date: 22 September 2014
* To compile: gcc -g -Wall -framework Foundation -o sorting sorting.m
*/
#import <Foundation/Foundation.h>
void printByAge(NSArray *people);
void printByName(NSArray *people);
void printNickNames(NSArray *people);
void printSimpsons(NSArray *people);
void findPersonOfAge(NSArray *people, int age);
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *people = @[@{@"firstname":@"Homer", @"lastname":@"Simpson", @"age":@(40)},
@{@"firstname":@"Marjorie", @"lastname":@"Simpson", @"age":@(37), @"nickname":@"Marge"},
@{@"firstname":@"Abraham", @"lastname":@"Simpson", @"age":@(83), @"nickname":@"Grandpa"},
@{@"firstname":@"Bart", @"lastname":@"Simpson", @"age":@(10), @"nickname":@"El Barto"},
@{@"firstname":@"Lisa", @"lastname":@"Simpson", @"age":@(8)},
@{@"firstname":@"Margaret", @"lastname":@"Simpson", @"age":@(1), @"nickname":@"Maggie"},
@{@"firstname":@"Charles", @"lastname":@"Burns", @"age":@(104), @"nickname":@"Monty"},
@{@"firstname":@"Herschel", @"lastname":@"Krustofsky", @"age":@(54), @"nickname":@"Krusty"}];
printByAge(people);
printByName(people);
printNickNames(people);
printSimpsons(people);
findPersonOfAge(people, 83);
findPersonOfAge(people, 943);
[pool release];
return (EXIT_SUCCESS);
}
void printByAge(NSArray *people) {
NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
NSArray *sortedNames = [people sortedArrayUsingDescriptors:@[ageDescriptor]];
NSLog(@"NAMES SORTED BY AGE (LOW TO HIGH)");
for (NSDictionary *person in sortedNames) {
NSLog(@"%@ %@ (%@)", person[@"firstname"], person[@"lastname"], person[@"age"]);
}
NSLog(@"-------------");
}
void printByName(NSArray *people) {
NSSortDescriptor *lastnameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastname" ascending:YES];
NSSortDescriptor *firstnameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES];
NSArray *sortedeNames = [people sortedArrayUsingDescriptors:@[lastnameDescriptor, firstnameDescriptor]];
NSLog(@"SORTED NAMES");
for (NSDictionary *person in sortedeNames) {
NSLog(@"%@, %@", person[@"lastname"], person[@"firstname"]);
}
NSLog(@"-------------");
}
void printNickNames(NSArray *people) {
NSPredicate *nicknamesPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return ([(NSDictionary *)evaluatedObject objectForKey:@"nickname"] != nil);
}];
NSArray *nicknames = [people filteredArrayUsingPredicate:nicknamesPredicate];
NSLog(@"CHARACTERS WITH NICKNAMES");
for (NSDictionary *person in nicknames) {
NSLog(@"%@ \"%@\" %@", person[@"firstname"], person[@"nickname"], person[@"lastname"]);
}
NSLog(@"-------------");
}
void printSimpsons(NSArray *people) {
NSPredicate *isASimpsonPredicate = [NSPredicate predicateWithFormat: @"lastname == %@", @"Simpson"];
NSArray *simpsons = [people filteredArrayUsingPredicate:isASimpsonPredicate];
NSLog(@"SIMPSON CHARACTERS");
for (NSDictionary *person in simpsons) {
NSLog(@"%@ %@", person[@"firstname"], person[@"lastname"]);
}
}
void findPersonOfAge(NSArray *people, int age) {
NSPredicate *predicateExists = [NSPredicate predicateWithFormat: @"age == %d", age];
NSUInteger index = [people indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop) {
return [predicateExists evaluateWithObject:obj];
}];
NSLog(@"-------------");
if (index == NSNotFound) {
NSLog(@"No Simpsons character is of age %d", age);
} else {
NSDictionary *person = [people objectAtIndex:index];
NSLog(@"%@ %@ is %d years old", person[@"firstname"], person[@"lastname"], age);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment