I have an array to which objects of different classes are add.
After that, this array is sorted by a date attribute which both classes have.
But the sorted array is not sorted in the correct order:
here is my code [somewhere in viewDid]:
NSMutableArray *syncedAufgabe = [[NSMutableArray alloc] initWithCapacity:0];
// loop through termine and fetch each event and add it to the events array
for (Aufgabe *aufgabe in objects) {
NSString *calendarItemIdentifier = aufgabe.calendarItemIdentifier;
EKReminder *reminder = (EKReminder *)[self.eventStore calendarItemWithIdentifier:calendarItemIdentifier];
if (reminder == nil) {
// remove the respective aufgabe
[[cdh context] deleteObject:aufgabe];
} else {
[syncedAufgabe addObject:aufgabe];
}
}
[cdh saveContext];
NSSet *impfVorgaenge = patient.impf_vorgaenge;
for (ImpfVorgang *impfVorgang in impfVorgaenge) {
NSString *calendarItemIdentifier = impfVorgang.calendar_item_identifier;
EKReminder *reminder = (EKReminder *)[self.eventStore calendarItemWithIdentifier:calendarItemIdentifier];
if (reminder == nil) {
// remove respective impfVorgang
[[cdh context] deleteObject:impfVorgang];
} else {
[syncedAufgabe addObject:impfVorgang];
}
}
[cdh saveContext];
NSArray * sortedSyncedAufgaben = [syncedAufgabe sortedArrayUsingComparator:^(NSObject *a, NSObject* b) {
return [self compare:a to:b];
}];
And this is the comparison method called from the block
-(NSComparisonResult)compare:(NSObject *)object to:(NSObject *) anotherObject{
NSDate *objectDate;
if ([object isKindOfClass:[Aufgabe class]]) {
Aufgabe *aufgabe = (Aufgabe *)object;
NSString *calendarItemIdentifier = aufgabe.calendarItemExternalIdentifier;
EKReminder *reminder = (EKReminder *)[self.eventStore calendarItemWithIdentifier:calendarItemIdentifier];
objectDate = [_gregorianCalendar dateFromComponents:reminder.dueDateComponents];
} else if ([object isKindOfClass:[ImpfVorgang class]]){
ImpfVorgang *impfVorgang = (ImpfVorgang *)object;
NSString *calendarItemIdentifier = impfVorgang.calendar_item_identifier;
EKReminder *reminder = (EKReminder *)[self.eventStore calendarItemWithIdentifier:calendarItemIdentifier];
objectDate = [_gregorianCalendar dateFromComponents:reminder.dueDateComponents];
}
NSDate *anotherObjectDate;
if ([anotherObject isKindOfClass:[Aufgabe class]]) {
Aufgabe *aufgabe = (Aufgabe *)anotherObject;
NSString *calendarItemIdentifier = aufgabe.calendarItemIdentifier;
EKReminder *reminder = (EKReminder *)[self.eventStore calendarItemWithIdentifier:calendarItemIdentifier];
anotherObjectDate = [_gregorianCalendar dateFromComponents:reminder.dueDateComponents];
} else if ([object isKindOfClass:[ImpfVorgang class]]){
ImpfVorgang *impfVorgang = (ImpfVorgang *)object;
NSString *calendarItemIdentifier = impfVorgang.calendar_item_identifier;
EKReminder *reminder = (EKReminder *)[self.eventStore calendarItemWithIdentifier:calendarItemIdentifier];
anotherObjectDate = [_gregorianCalendar dateFromComponents:reminder.dueDateComponents];
}
return [objectDate compare:anotherObjectDate];
}
In a nutshell, I extract the date depending on which class the object is, and do a comparison.
However, the aufgabe objects end up sorted correctly at the beginning of the array, and the impfVorgang objects at the end, but not sorted correctly.
What I want to achieve, is that all objects, independed of their class, are chronologically sorted.
Thanks for any help.