我目前正在使用属性startTime
对对象数组进行排序,但我已经意识到它是按日期和时间排序的。
因此,如果我添加一个对象并将时间设置为早于预先制作的对象,如果超过一天后它将始终列在另一个之后。有意义吗?
让我们说我的NSDate是星期二25日下午6:02。我想划破星期二的第26位,只是按时间排序,这一天对我来说无关紧要。我怎么能这样做?
感谢。
P.S这就是我目前正在排序对象的方式:
NSSet *tasks = [self.routine task];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:YES];
NSArray *sorted = [[tasks allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
答案 0 :(得分: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" )
答案 1 :(得分: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创建的。