6

我需要对NSArray包含时间NSString的内容进行排序,例如,

NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];

我需要的是按时间升序对数组进行排序。
有没有办法做这样的事情?

4

4 回答 4

10
NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];

NSArray *sortedTimes = [times sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2)
{
    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];
    return [date1 compare:date2];
}];

优化版:

NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];

NSMutableArray *dates = [NSMutableArray arrayWithCapacity:times.count];
for (NSString *timeString in times)
{
    NSDate *date = [dateFormatter dateFromString:timeString];
    [dates addObject:date];
}

[dates sortUsingSelector:@selector(compare:)];

NSMutableArray *sortedTimes = [NSMutableArray arrayWithCapacity:dates.count];
for (NSDate *date in dates)
{
    NSString *timeString = [dateFormatter stringFromDate:date];
    [sortedTimes addObject:timeString];
}
于 2013-04-25T07:29:58.040 回答
4

你可以试试这段代码:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"hh:mm a"];
    [times sortUsingComparator:^NSComparisonResult(NSString* obj1, NSString *obj2) {
        NSDate *firstDate = [formatter dateFromString:obj1];
        NSDate *secondDate = [formatter dateFromString:obj2];
        return [firstDate compare:secondDate];
    }];
于 2013-04-25T07:34:39.480 回答
3

由于这些是字符串,它将被排序为

01:15, 12:25, 05:00....

他们也不是NSDate

所以你需要做的是从这些字符串中创建一个具有 NSDate 的并行数组,对数组进行排序,然后提取这些值。


在实施时,我通过新手方式解决了它

NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"09:00 AM",@"07:30 AM",@"06:45 PM",@"05:00 PM",@"12:45 AM",@"12:45 PM",@"01:00 AM",@"01:15 PM", nil];

NSMutableArray *dates=[NSMutableArray new];

NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"hh:mm a"];

for (NSString *stringDate in times) {
        NSDate *date=[dateFormatter dateFromString:stringDate];
        [dates addObject:date];
}

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject: descriptor];
NSArray *reverseOrder = [dates sortedArrayUsingDescriptors:descriptors];

[times removeAllObjects];
for (NSDate *date in reverseOrder) {
        NSString *string=[dateFormatter stringFromDate:date];
        [times addObject:string];
}

NSLog(@"%@",times);
于 2013-04-25T07:18:45.523 回答
0

对于 NSDate 比较使用这个:

+ (BOOL)isDate:(NSDate *)date1 smallerThanAnotherDate:(NSDate *)date2
{
    NSDate* enddate = date1;
    NSDate* currentdate = date2;
    NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
    double secondsInMinute = 60;
    NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;

    if (secondsBetweenDates == 0)
        return YES;
    else if (secondsBetweenDates < 0)
        return YES;
    else
        return NO;
}

因此,如果您不想将您的时间转换为 NSDate,则单独执行此操作对我有用

NSArray *sortedTimes =  [times sortedArrayUsingSelector:@selector(localizedStandardCompare:)];
于 2013-04-25T07:19:55.407 回答