Created
May 23, 2015 08:28
-
-
Save palmerc/014ad92fbefe7df0c94d to your computer and use it in GitHub Desktop.
Obj-C Command-line Sort Example
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
| #import <Foundation/Foundation.h> | |
| #define randomNumber() (arc4random() % ((unsigned)1000000 + 1)) | |
| @interface MainHelper : NSObject | |
| + (NSArray *)largeArrayCreate; | |
| @end | |
| @implementation MainHelper | |
| + (NSArray *)largeArrayCreate | |
| { | |
| int sizeOfArray = 40000; | |
| NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:sizeOfArray]; | |
| for (int i=0; i < sizeOfArray; i++) { | |
| NSInteger value = randomNumber(); | |
| NSString *string = [NSString stringWithFormat:@"%ld", value]; | |
| [mutableArray addObject:string]; | |
| } | |
| return [mutableArray copy]; | |
| } | |
| @end | |
| int main(int argc, char *argv[]) | |
| { | |
| @autoreleasepool { | |
| NSArray *array1 = [MainHelper largeArrayCreate]; | |
| NSArray *array2 = [MainHelper largeArrayCreate]; | |
| NSLog(@"Array 1: %ld, Array 2: %ld", [array1 count], [array2 count]); | |
| NSMutableSet *mutableSet = [[NSMutableSet alloc] initWithArray:array1]; | |
| [mutableSet addObjectsFromArray:array2]; | |
| NSArray *result = [[mutableSet allObjects] sortedArrayUsingSelector:@selector(compare:)]; | |
| NSLog(@"Resulting array size %ld", [result count]); | |
| } | |
| return 0; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile
cc -ObjC -framework Foundation sort.m -o sort
Run
./sort