iOS engineer who likes clean, simple designs and sans-serif.

Loading

Week 2 Day 2 – NSSortDescriptor and NSPredicate

Here’s an example on how to use (more than one) NSSortDescriptor. It seems equivalent to a SQL “sort by”.

NSArray *instructors = @[joe,al,babyAl];
NSSortDescriptor *nameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
NSSortDescriptor *heightDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"height" ascending:YES];

NSLog(@"%@",[instructors sortedArrayUsingDescriptors:@[nameDescriptor,heightDescriptor]]);

Here’s an example of an NSPredicate, which seems like the equivalent of SQL “where”:

NSArray *instructors = @[joe,al,babyAl];  
NSPredicate *searchForAl = [NSPredicate predicateWithFormat:@"height ==%@",@180];
NSLog(@"%@",[instructors filteredArrayUsingPredicate:searchForAl]);

Leave a Comment