Skip to content

Instantly share code, notes, and snippets.

@taylanpince
Created May 2, 2012 08:31
Show Gist options
  • Save taylanpince/2575090 to your computer and use it in GitHub Desktop.
Save taylanpince/2575090 to your computer and use it in GitHub Desktop.
NSArray reorder
static NSInteger compareListingTypesAccordingToStaticOrder(ListingType *type1, ListingType *type2, void *context) {
static NSArray *orderedTypes = nil;
if (orderedTypes == nil) {
orderedTypes = [[NSArray alloc] initWithObjects:
@"Everything", @"Restaurants", @"Bars",
@"Cafes", @"Bakeries", @"Fashion Stores",
@"Design Stores", @"Bookstores", @"Art Galleries",
@"Fitness Clubs", @"Grocery", @"Hotels", @"Services", nil];
}
NSInteger order1 = [orderedTypes indexOfObject:type1.categoryTitle];
NSInteger order2 = [orderedTypes indexOfObject:type2.categoryTitle];
if (order1 < order2) {
return (order1 == NSNotFound) ? NSOrderedDescending : NSOrderedAscending;
} else if (order1 > order2) {
return (order2 == NSNotFound) ? NSOrderedAscending : NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
[listingTypes sortUsingFunction:compareListingTypesAccordingToStaticOrder context:NULL];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment