iosnssortdescriptornsdatecomponentseventkitekeventkit

Sort EKReminder based on due date


I have an array of EKReminder objects and I would like to sort them by due date.

This seems easy at first, doing something like this:

NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dueDate" ascending:YES];

[remindersArray sortUsingDescriptors:@[dateDescriptor]];

However the due date of an EKReminder is an NSDateComponents object not an NSDate.

Any ideas?


Solution

  • So, after some research I managed to solve it relatively easy:

    [allReminders sortUsingComparator:^NSComparisonResult(id obj1,id obj2){
            EKReminder *reminder1 = obj1;
            EKReminder *reminder2 = obj2;
    
            NSDate *date1 = [reminder1.dueDateComponents date];
            NSDate *date2 = [reminder2.dueDateComponents date];
            return [date1 compare:date2];
         }];
    

    Hope this helps someone!