我有一个次数(HH:mm:ss
),如何找出哪个时间更大,哪个时间过去了?如果我可以通过提升来对它们进行排序,那将会很棒。
答案 0 :(得分:1)
如果您的时间是以HH:mm:ss
格式的字符串给出的,您可以将它们转换为NSDate对象并使用可变数组对它们进行排序:
NSArray *timeStrings = // however you obtain it...
NSMutableArray *sorted = [NSMutableArray array];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"HH:mm:ss"];
for (NSString *dtStr in timeStrings) {
[sorted addObject:[fmt dateFromString:dtStr]];
}
[sorted sortUsingSelector:@selector(compare:)];
现在sorted
将包含按asccending排序的日期对象。
答案 1 :(得分:0)
//COnvert Strings to Dates
//Comparison with current time
NSMutableArray *dates;
for (NSDate *d in dates) {
if ([d compare:[NSDate date]]== NSOrderedAscending) {
NSLog(@"Future Time");
}else if([d compare:[NSDate date]]== NSOrderedSame){
NSLog(@"Present Time");
}else{
NSLog(@"Past Time");
}
}
// For sorting
[dates sortUsingSelector:@selector(compare:)];