2011-12-28 35 views
2

我有一個數組控制器與nextCheck(NSDate),lastName(NSString)和firstName(NSString)的鍵。我已經爲數組設置了排序描述符。正如你在下面看到的,我先通過nextCheck排序,然後是lastName,然後是firstName。這出現不工作,因爲NSDate不僅是一個日期組件,也是一個時間組件。有沒有簡單的方法來按日期組件排序?排序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]]; 

回答

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]]; 
2

我所做的,是當我創建日期,我只用了日期的日期組件:

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *comps = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) 
              fromDate:[NSDate date]]; 
NSDate *currentDate = [calendar dateFromComponents:comps]; 
+0

感謝這兩個答案。我將使用其中之一。 – docwelch 2011-12-28 18:18:44

相關問題