Created
October 21, 2012 18:56
-
-
Save itaiferber/3928086 to your computer and use it in GitHub Desktop.
Demonstrates nested sorting using NSSortDescriptors.
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
#import <Cocoa/Cocoa.h> | |
@interface IFEntry : NSObject {} | |
@property (copy, nonatomic) NSString *company; | |
@property (strong, nonatomic) NSDate *date; | |
- (instancetype)initWithCompany:(NSString *)company date:(NSDate *)date; | |
@end | |
@implementation IFEntry {} | |
- (instancetype)initWithCompany:(NSString *)company date:(NSDate *)date | |
{ | |
if (self != [super init]) { | |
return nil; | |
} | |
_company = [company copy]; | |
_date = date; | |
return self; | |
} | |
- (NSString *)description | |
{ | |
return [NSString stringWithFormat:@"%@ - %@", _company, _date]; | |
} | |
@end | |
int main(int argc, char *argv[]) | |
{ | |
@autoreleasepool { | |
NSArray *entries = @[ | |
[[IFEntry alloc] initWithCompany:@"Apple" date:[NSDate date]], | |
[[IFEntry alloc] initWithCompany:@"Dell" date:[NSDate date]], | |
[[IFEntry alloc] initWithCompany:@"Amazon" date:[NSDate date]], | |
[[IFEntry alloc] initWithCompany:@"Apple" date:[NSDate dateWithNaturalLanguageString:@"yesterday"]] | |
]; | |
NSArray *descriptors = @[ | |
[NSSortDescriptor sortDescriptorWithKey:@"company" ascending:YES], | |
[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES] | |
]; | |
NSLog(@"%@", [entries sortedArrayUsingDescriptors:descriptors]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment