gpt4 book ai didi

ios - 仅使用时间按日期排序数组?

转载 作者:可可西里 更新时间:2023-11-01 06:20:57 24 4
gpt4 key购买 nike

我目前正在对具有属性 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]];

最佳答案

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" )

关于ios - 仅使用时间按日期排序数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com