Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Please make sure your dateFromComponents is correctly returned in impfVorgang objects –  Alexander Tkachenko Jun 6 at 13:28
    
@AlexanderTkachenko good hint, i added some NSLog statements and this seems to be the case, some impfVorgang objects don't have a date, have to figure out why... –  mradlmaier Jun 6 at 13:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.