Сортировка NSDate без учета времени суток - только дата

У меня есть контроллер массива с ключами nextCheck (NSDate), lastName (NSString) и firstName (NSString). Я установил дескрипторы сортировки для массива. Как вы можете видеть ниже, я сортирую сначала по nextCheck, затем по lastName, затем по firstName. Этот кажется не работает, потому что NSDate состоит не только из компонента даты, но и из компонента времени. Есть ли простой способ сортировки только по компоненту даты?

[_arrayCurrentVisits setSortDescriptors:[NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"nextVisit" ascending:YES selector:@selector(compare:)],[NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)], [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)], nil]];

person docwelch    schedule 28.12.2011    source источник


Ответы (2)


Самым простым решением было бы добавить метод под названием nextVisitDate возвращает значение nextVisit "усечено" до полуночи и вместо этого выполняет сортировку по nextVisitDate.

[_arrayCurrentVisits setSortDescriptors:[ //                           vvv HERE vvv
    NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"nextVisitDate"  ascending:YES selector:@selector(compare:)]
,   [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]
,   [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]
,   nil]];
person Sergey Kalinichenko    schedule 28.12.2011

Что я сделал, так это то, что когда я создал даты, я использовал только компоненты даты:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) 
                                          fromDate:[NSDate date]];
NSDate *currentDate = [calendar dateFromComponents:comps];
person agilityvision    schedule 28.12.2011
comment
Спасибо за оба ответа. Я воспользуюсь одним из них. - person docwelch; 28.12.2011