Created
December 3, 2012 11:12
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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