Skip to content

Instantly share code, notes, and snippets.

@palmerc
Created May 23, 2015 08:28
Show Gist options
  • Select an option

  • Save palmerc/014ad92fbefe7df0c94d to your computer and use it in GitHub Desktop.

Select an option

Save palmerc/014ad92fbefe7df0c94d to your computer and use it in GitHub Desktop.
Obj-C Command-line Sort Example
#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;
}
@palmerc
Copy link
Copy Markdown
Author

palmerc commented May 23, 2015

Compile

cc -ObjC -framework Foundation sort.m -o sort

Run

./sort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment