I am pushing 3 different entities in a NSMutableArray
, and need to compare them on the date.
But the 3 different entities have different names for the date.
I use the 3 entities: Birthday, Events, OtherEvents with the date properties: birthday, day, date.
Does anyone has any idea how to sort it like this. It has about 40 properties in it so its not a single comparison.
Thanks in advance
Try this:
[eventsArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDate *date1 = nil;
NSDate *date2 = nil;
if ([obj1 isKindOfClass:[Birthday class]]) {
date1 = [obj1 birthday];
} else if ([obj1 isKindOfClass:[Events class]) {
date1 = [obj1 day];
} else if ([obj1 isKindOfClass:[OtherEvents class]) {
date1 = [obj1 date];
}
if ([obj2 isKindOfClass:[Birthday class]]) {
date2 = [obj2 birthday];
} else if ([obj2 isKindOfClass:[Events class]) {
date2 = [obj2 day];
} else if ([obj isKindOfClass:[OtherEvents class]) {
date2 = [obj2 date];
}
return [date1 compare:date2];
}];