Created
May 28, 2014 18:03
-
-
Save vikingosegundo/a39e09d073f664c20811 to your computer and use it in GitHub Desktop.
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 <Foundation/Foundation.h> | |
@interface Birthday : NSObject | |
@property (nonatomic, strong) NSDate *birthday; | |
@property (nonatomic, copy) NSString *name; | |
-(id)initWithName:(NSString *) name birthdayDate:(NSDate *) date; | |
@end | |
@implementation Birthday | |
-(id)initWithName:(NSString *) name birthdayDate:(NSDate *) date | |
{ | |
if(self = [super init]){ | |
_birthday = date; | |
_name = name; | |
}; | |
return self; | |
} | |
-(NSString *)description | |
{ | |
return [NSString stringWithFormat:@"Birthday:\t %@: %@", _birthday, _name]; | |
} | |
@end | |
@interface Event : NSObject | |
@property (nonatomic, strong) NSDate *day; | |
@property (nonatomic, copy) NSString *title; | |
-(id)initWithTitle:(NSString *) title date:(NSDate *) date; | |
@end | |
@implementation Event | |
-(id)initWithTitle:(NSString *) title date:(NSDate *) date; | |
{ | |
if(self = [super init]){ | |
_day = date; | |
_title = title; | |
}; | |
return self; | |
} | |
-(NSString *)description | |
{ | |
return [NSString stringWithFormat:@"Event:\t\t %@: %@", _day, _title]; | |
} | |
@end | |
@interface OtherEvent : NSObject | |
@property (nonatomic, strong) NSDate *date; | |
@property (nonatomic, copy) NSString *title; | |
-(id)initWithTitle:(NSString *) title date:(NSDate *) date; | |
@end | |
@implementation OtherEvent | |
-(id)initWithTitle:(NSString *) title date:(NSDate *) date; | |
{ | |
if(self = [super init]){ | |
_date = date; | |
_title = title; | |
}; | |
return self; | |
} | |
-(NSString *)description | |
{ | |
return [NSString stringWithFormat:@"OtherEvent: %@: %@", _date, _title]; | |
} | |
@end | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSMutableArray *array = [@[] mutableCopy]; | |
[array addObject:[[Birthday alloc] initWithName:@"Bibo" | |
birthdayDate:( | |
{ | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.year = 1969; | |
comps.month = 7; | |
comps.day = 15; | |
[[NSCalendar currentCalendar] dateFromComponents:comps]; | |
}) | |
]]; | |
[array addObject:[[Birthday alloc] initWithName:@"Jimi Hendrix" | |
birthdayDate:( | |
{ | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.year = 1942; | |
comps.month = 11; | |
comps.day = 27; | |
[[NSCalendar currentCalendar] dateFromComponents:comps]; | |
}) | |
]]; | |
[array addObject:[[Event alloc] initWithTitle:@"Vacation end" | |
date:( | |
{ | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.year = 2014; | |
comps.month = 6; | |
comps.day = 21; | |
[[NSCalendar currentCalendar] dateFromComponents:comps]; | |
}) | |
]]; | |
[array addObject:[[Birthday alloc] initWithName:@"Samson" | |
birthdayDate:( | |
{ | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.year = 1977; | |
comps.month = 9; | |
comps.day = 3; | |
[[NSCalendar currentCalendar] dateFromComponents:comps]; | |
}) | |
]]; | |
[array addObject:[[Event alloc] initWithTitle:@"Vacation begin" | |
date:( | |
{ | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.year = 2014; | |
comps.month = 6; | |
comps.day = 1; | |
[[NSCalendar currentCalendar] dateFromComponents:comps]; | |
}) | |
]]; | |
[array addObject:[[OtherEvent alloc] initWithTitle:@"Woodstock Music and Art Festival" | |
date:( | |
{ | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.year = 1969; | |
comps.month = 15; | |
comps.day = 8; | |
[[NSCalendar currentCalendar] dateFromComponents:comps]; | |
}) | |
]]; | |
NSDictionary *dateMapper = @{NSStringFromClass([Birthday class]): @"birthday", | |
NSStringFromClass([Event class]): @"day", | |
NSStringFromClass([OtherEvent class]): @"date"}; | |
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { | |
NSDate *date1 = [obj1 valueForKey: dateMapper[NSStringFromClass([obj1 class])]]; | |
NSDate *date2 = [obj2 valueForKey: dateMapper[NSStringFromClass([obj2 class])]]; | |
return [date1 compare:date2]; | |
}]; | |
for (id obj in array) { | |
NSLog(@"%@", obj); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment