Created
April 8, 2015 14:36
-
-
Save jeffreyjackson/bfbc8f68e4cb1a201fd9 to your computer and use it in GitHub Desktop.
Merging 2 NSArrays & Duplicates
This file contains 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
//You have two NSArrays and you want to merge them into 1 Array. | |
//Easy. You need to know about NSSet and maybe a little 'Set Theory'. | |
NSMutableSet *mergedSet = [NSMutableSet setWithArray:arrayFirst]; | |
[mergedSet unionSet:[NSSet setWithArray:arraySecond]]; | |
//In Set Theory, a union will automatically remove your duplicates. In the case you only wanted to know about duplicates, then take a look at intersectSet: | |
//Take your new set one step further by sorting them alphabetically with a block: | |
arraySorted = [[mergedSet allObjects] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { | |
NSString *first = [a objectForKey:@"identifier"]; | |
NSString *second = [b objectForKey:@"identifier"]; | |
return [first compare:second]; | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment