我有一个带有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]];
答案 0 :(得分:4)
最简单的解决方案是add a method called 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]];
答案 1 :(得分:2)
我做的是,当我创建日期时,我只使用日期的日期组件:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit)
fromDate:[NSDate date]];
NSDate *currentDate = [calendar dateFromComponents:comps];