3

我目前正在对具有属性的对象数组进行排序,startTime但是我意识到它是按日期和时间排序的。

因此,如果我添加一个对象并将时间设置为比预制对象更早的时间,如果它在一天之后制造,它将始终排在另一个之后。有道理?

所以假设我的 NSDate 是星期二 25 日下午 6:02。我想从 26 号星期二开始,只按时间排序,这一天对我来说并不重要。我怎样才能做到这一点?

谢谢。

PS这就是我目前对我的对象进行排序的方式:

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

2 回答 2

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" )
于 2013-01-15T22:56:38.963 回答
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 中创建的。

于 2013-01-15T22:55:32.420 回答