Skip to content

Instantly share code, notes, and snippets.

@itaiferber
Created October 21, 2012 18:56
Show Gist options
  • Save itaiferber/3928086 to your computer and use it in GitHub Desktop.
Save itaiferber/3928086 to your computer and use it in GitHub Desktop.
Demonstrates nested sorting using NSSortDescriptors.
#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