2013-01-15 58 views
3

我目前正在用屬性startTime排序一個對象數組,但是我意識到它是按日期和時間排序的。按日期排序數組僅使用時間?

因此,如果我添加一個對象並將時間設置爲比預先創建的對象早,那麼如果它超過一天之後它將一直列在另一個之後。合理?

讓我們說我的NSDate是星期二25日6:02 PM。我想抓住週二的第26位,只按時間排序,這一天對我來說並不重要。我怎樣才能做到這一點?

謝謝。

P.S這是我當前如何整理我的對象:

NSSet *tasks = [self.routine task]; 
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:YES]; 
    NSArray *sorted = [[tasks allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]; 

回答

7

NSDate上創建一個類別,該類別返回當天日曆和時區開始一天以來的時間間隔,然後使用該時間間隔進行排序。

@interface NSDate (CBVAdditions) 

- (NSTimeInterval)cbvTimeIntervalSinceStartOfDay; 

@end 

@implementation NSDate (CBVAdditions) 

- (NSTimeInterval)cbvTimeIntervalSinceStartOfDay 
{ 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSTimeZone *timeZone = [NSTimeZone defaultTimeZone]; 
    NSDateComponents *dateComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self]; 
    [dateComponents setCalendar:calendar]; 
    [dateComponents setTimeZone:timeZone]; 
    NSInteger hoursComponent = dateComponents.hour * 3600; 
    NSInteger minutesComponent = dateComponents.minute * 60; 
    double secondsComponent = dateComponents.second; 
    NSTimeInterval toReturn = hoursComponent + minutesComponent + secondsComponent; 
    return toReturn; 
} 

@end 

然後,您可以使用NSSortDescriptor用鑰匙像@"dateProperty.cbvTimeIntervalSinceStartOfDay"做實際的排序。例如,我展示了Event類,以及一些完成該類的代碼。

@interface Event : NSObject 

@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSDate *eventDate; 

@end 

@implementation Event 

- (NSString *)description 
{ 
    NSString *desc = [NSString stringWithFormat:@"%@, name = %@, eventDate = %@",[super description], self.name, [self.eventDate descriptionWithLocale:[NSLocale currentLocale]]]; 
    return desc; 
} 
@end 

... 

Event *e1 = [Event new]; 
e1.eventDate = [NSDate date]; 
e1.name = @"e1"; 
Event *e2 = [Event new]; 
e2.eventDate = [NSDate dateWithTimeIntervalSinceNow:(-1 * 22 * 3600)]; 
e2.name = @"e2"; 
Event *e3 = [Event new]; 
e3.eventDate = [NSDate dateWithTimeIntervalSinceNow:3600]; 
e3.name = @"e3"; 

NSArray *events = @[e1,e2,e3]; 
NSLog(@"Events = %@", events); 
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"eventDate.cbvTimeIntervalSinceStartOfDay" ascending:YES]; 
NSArray *sortedEvents = [events sortedArrayUsingDescriptors:@[sortDescriptor]]; 
NSLog(@"Sorted Events = %@", sortedEvents); 

執行此代碼提出了以下的輸出:

2013-01-15 15:50:54.221 DateSortingFun[67319:c07] Events = (
    "<Event: 0x717b050>, name = e1, eventDate = Tuesday, January 15, 2013, 3:50:54 PM Mountain Standard Time", 
    "<Event: 0x717b620>, name = e2, eventDate = Monday, January 14, 2013, 5:50:54 PM Mountain Standard Time", 
    "<Event: 0x717b650>, name = e3, eventDate = Tuesday, January 15, 2013, 4:50:54 PM Mountain Standard Time") 


2013-01-15 15:50:54.222 DateSortingFun[67319:c07] Sorted Events = (
    "<Event: 0x717b050>, name = e1, eventDate = Tuesday, January 15, 2013, 3:50:54 PM Mountain Standard Time", 
    "<Event: 0x717b650>, name = e3, eventDate = Tuesday, January 15, 2013, 4:50:54 PM Mountain Standard Time", 
    "<Event: 0x717b620>, name = e2, eventDate = Monday, January 14, 2013, 5:50:54 PM Mountain Standard Time") 
+0

真棒事情@Carl – channi

4

使用比較塊。並在該塊中創建一個年份,月份和日期設置爲同一天的新數據。在那些日期做一個正常的比較。

NSDateComponents *comps = [[NSDateComponents alloc] init]; 
    comps.year = 2007; 
    comps.month = 12; 
    comps.day = 3; 
    comps.hour = 12; 
    comps.minute = 3; 

    NSDate *date1 = [[NSCalendar currentCalendar] dateFromComponents:comps]; 

    comps.year = 2012; 
    comps.month = 9; 
    comps.day = 7; 
    comps.hour = 11; 
    comps.minute = 13; 

    NSDate *date2 = [[NSCalendar currentCalendar] dateFromComponents:comps]; 

    comps.year = 2021; 
    comps.month = 12; 
    comps.day = 3; 
    comps.hour = 12; 
    comps.minute = 5; 

    NSDate *date3 = [[NSCalendar currentCalendar] dateFromComponents:comps]; 

    comps.year = 2021; 
    comps.month = 12; 
    comps.day = 3; 
    comps.hour = 13; 
    comps.minute = 5; 


    NSDate *date4 = [[NSCalendar currentCalendar] dateFromComponents:comps]; 

    NSArray *dates = @[date1, date2, date3, date4]; 

    NSArray *oderedByTimeOnlyDates = [dates sortedArrayUsingComparator:^NSComparisonResult(NSDate *d1, NSDate *d2) { 
     NSDateComponents *comps1 = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit| NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                    fromDate:d1]; 
     NSDateComponents *comps2 = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit| NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                    fromDate:d2]; 

     NSDate *date1 = [[NSCalendar currentCalendar] dateFromComponents:comps1]; 
     NSDate *date2 = [[NSCalendar currentCalendar] dateFromComponents:comps2]; 
     return [date1 compare:date2]; 
    }]; 

    NSLog(@"%@", oderedByTimeOnlyDates); 

輸出:

(
    "2012-09-07 09:13:00 +0000", 
    "2007-12-03 11:03:00 +0000", 
    "2021-12-03 11:05:00 +0000", 
    "2021-12-03 12:05:00 +0000" 
) 

輸出是UTC時間,而它們在UTC + 1產生。