Skip to content

Instantly share code, notes, and snippets.

@benvium
Created December 3, 2012 11:12
Show Gist options
  • Select an option

  • Save benvium/4194249 to your computer and use it in GitHub Desktop.

Select an option

Save benvium/4194249 to your computer and use it in GitHub Desktop.
Example of using sortedArrayUsingComparator. idsInOrder has the order that the objects should be in. Actual objects may or may not be in the ordered list.
NSArray* idsInOrder = @[@"foo", @"bar", @"baz", @"badgers"];
NSArray* loadedStuff = @[@{@"id":@"baz"}, @{@"id":@"badgers"}, @{@"id":@"wow"}, @{@"id":@"foo"}];
NSLog(@"UNORDERED: %@", loadedStuff);
NSArray* sorted = [loadedStuff sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* obj1, NSDictionary* obj2) {
// sort by position of id string in order array..
NSUInteger obj1Pos = [idsInOrder indexOfObject:obj1[@"id"] ];
NSUInteger obj2Pos = [idsInOrder indexOfObject:obj2[@"id"] ];
return [[NSNumber numberWithInteger:obj1Pos] compare:[NSNumber numberWithInteger:obj2Pos]];
}];
NSLog(@"ORDERED: %@", sorted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment