sortedArrayUsingComparator返回不正确的结果

时间:2014-06-22 06:02:38

标签: ios iphone objective-c nsdate nsdateformatter

我有一个数组,它有一组时间戳(作为字符串),它们按排序顺序排列。我只想再次对它们进行排序,看看是否返回了相同的列表(原因是我将处理无序存储的更多时间戳,并需要对它们进行排序)。这是我的日期数组,其中时间戳基本上是字符串:

(
"Jun 18, 2014 12:00:00 AM",
"Jun 18, 2014 12:15:00 AM",
"Jun 18, 2014 12:30:00 AM",
"Jun 18, 2014 12:45:00 AM",
"Jun 18, 2014 1:00:00 AM",
"Jun 18, 2014 1:15:00 AM",
"Jun 18, 2014 1:30:00 AM",
"Jun 18, 2014 1:45:00 AM",
"Jun 18, 2014 2:00:00 AM",
"Jun 18, 2014 2:15:00 AM",
"Jun 18, 2014 2:30:00 AM",
"Jun 18, 2014 2:45:00 AM",
"Jun 18, 2014 3:00:00 AM",
"Jun 18, 2014 3:15:00 AM",
"Jun 18, 2014 3:30:00 AM",
"Jun 18, 2014 4:15:00 AM",
"Jun 18, 2014 4:30:00 AM",
"Jun 18, 2014 4:45:00 AM",
"Jun 18, 2014 5:15:00 AM",
"Jun 18, 2014 5:30:00 AM",
"Jun 18, 2014 5:45:00 AM",
"Jun 18, 2014 6:00:00 AM",
"Jun 18, 2014 6:15:00 AM",
"Jun 18, 2014 6:30:00 AM",
"Jun 18, 2014 6:45:00 AM",
"Jun 18, 2014 7:00:00 AM",
"Jun 18, 2014 7:15:00 AM",
"Jun 18, 2014 7:30:00 AM",
"Jun 18, 2014 7:45:00 AM",
"Jun 18, 2014 8:00:00 AM",
"Jun 18, 2014 8:15:00 AM",
"Jun 18, 2014 8:30:00 AM",
"Jun 18, 2014 8:45:00 AM",
"Jun 18, 2014 9:00:00 AM",
"Jun 18, 2014 9:15:00 AM",
"Jun 18, 2014 10:00:00 AM",
"Jun 18, 2014 10:15:00 AM",
"Jun 18, 2014 10:30:00 AM",
"Jun 18, 2014 10:45:00 AM",
"Jun 18, 2014 11:15:00 AM",
"Jun 18, 2014 11:30:00 AM",
"Jun 18, 2014 11:45:00 AM",
"Jun 18, 2014 12:00:00 PM",
"Jun 18, 2014 12:15:00 PM",
"Jun 18, 2014 12:30:00 PM",
"Jun 18, 2014 12:45:00 PM",
"Jun 18, 2014 1:00:00 PM",
"Jun 18, 2014 1:15:00 PM",
"Jun 18, 2014 1:30:00 PM",
"Jun 18, 2014 2:00:00 PM",
"Jun 18, 2014 2:15:00 PM",
"Jun 18, 2014 2:30:00 PM",
"Jun 18, 2014 2:45:00 PM",
"Jun 18, 2014 3:00:00 PM",
"Jun 18, 2014 3:15:00 PM",
"Jun 18, 2014 3:45:00 PM",
"Jun 18, 2014 4:00:00 PM",
"Jun 18, 2014 4:15:00 PM",
"Jun 18, 2014 4:45:00 PM",
"Jun 18, 2014 5:15:00 PM",
"Jun 18, 2014 5:30:00 PM",
"Jun 18, 2014 6:00:00 PM",
"Jun 18, 2014 6:15:00 PM",
"Jun 18, 2014 7:00:00 PM",
"Jun 18, 2014 7:15:00 PM",
"Jun 18, 2014 7:30:00 PM",
"Jun 18, 2014 8:00:00 PM",
"Jun 18, 2014 8:15:00 PM",
"Jun 18, 2014 8:30:00 PM",
"Jun 18, 2014 9:15:00 PM",
"Jun 18, 2014 9:30:00 PM",
"Jun 18, 2014 9:45:00 PM",
"Jun 18, 2014 10:15:00 PM",
"Jun 18, 2014 10:30:00 PM",
"Jun 18, 2014 11:15:00 PM",
"Jun 18, 2014 11:30:00 PM",
"Jun 18, 2014 11:45:00 PM"
)

以上数组位于已排序的时间戳中。现在,如果我按升序对它们进行重新排序,它们应该给我相同的结果,我认为但是我的结果会出错。我只是不明白为什么我会看到这样的行为: 这是我得到的输出:

sorted dates = (
"Jun 18, 2014 12:00:00 AM",
"Jun 18, 2014 1:00:00 AM",
"Jun 18, 2014 2:00:00 AM",
"Jun 18, 2014 3:00:00 AM",
"Jun 18, 2014 6:00:00 AM",
"Jun 18, 2014 7:00:00 AM",
"Jun 18, 2014 8:00:00 AM",
"Jun 18, 2014 9:00:00 AM",
"Jun 18, 2014 10:00:00 AM",
"Jun 18, 2014 12:15:00 AM",
"Jun 18, 2014 1:15:00 AM",
"Jun 18, 2014 2:15:00 AM",
"Jun 18, 2014 3:15:00 AM",
"Jun 18, 2014 4:15:00 AM",
"Jun 18, 2014 5:15:00 AM",
"Jun 18, 2014 6:15:00 AM",
"Jun 18, 2014 7:15:00 AM",
"Jun 18, 2014 8:15:00 AM",
"Jun 18, 2014 9:15:00 AM",
"Jun 18, 2014 10:15:00 AM",
"Jun 18, 2014 11:15:00 AM",
"Jun 18, 2014 12:30:00 AM",
"Jun 18, 2014 1:30:00 AM",
"Jun 18, 2014 2:30:00 AM",
"Jun 18, 2014 3:30:00 AM",
"Jun 18, 2014 4:30:00 AM",
"Jun 18, 2014 5:30:00 AM",
"Jun 18, 2014 6:30:00 AM",
"Jun 18, 2014 7:30:00 AM",
"Jun 18, 2014 8:30:00 AM",
"Jun 18, 2014 10:30:00 AM",
"Jun 18, 2014 11:30:00 AM",
"Jun 18, 2014 12:45:00 AM",
"Jun 18, 2014 1:45:00 AM",
"Jun 18, 2014 2:45:00 AM",
"Jun 18, 2014 4:45:00 AM",
"Jun 18, 2014 5:45:00 AM",
"Jun 18, 2014 6:45:00 AM",
"Jun 18, 2014 7:45:00 AM",
"Jun 18, 2014 8:45:00 AM",
"Jun 18, 2014 10:45:00 AM",
"Jun 18, 2014 11:45:00 AM",
"Jun 18, 2014 12:00:00 PM",
"Jun 18, 2014 1:00:00 PM",
"Jun 18, 2014 2:00:00 PM",
"Jun 18, 2014 3:00:00 PM",
"Jun 18, 2014 4:00:00 PM",
"Jun 18, 2014 6:00:00 PM",
"Jun 18, 2014 7:00:00 PM",
"Jun 18, 2014 8:00:00 PM",
"Jun 18, 2014 12:15:00 PM",
"Jun 18, 2014 1:15:00 PM",
"Jun 18, 2014 2:15:00 PM",
"Jun 18, 2014 3:15:00 PM",
"Jun 18, 2014 4:15:00 PM",
"Jun 18, 2014 5:15:00 PM",
"Jun 18, 2014 6:15:00 PM",
"Jun 18, 2014 7:15:00 PM",
"Jun 18, 2014 8:15:00 PM",
"Jun 18, 2014 9:15:00 PM",
"Jun 18, 2014 10:15:00 PM",
"Jun 18, 2014 11:15:00 PM",
"Jun 18, 2014 12:30:00 PM",
"Jun 18, 2014 1:30:00 PM",
"Jun 18, 2014 2:30:00 PM",
"Jun 18, 2014 5:30:00 PM",
"Jun 18, 2014 7:30:00 PM",
"Jun 18, 2014 8:30:00 PM",
"Jun 18, 2014 9:30:00 PM",
"Jun 18, 2014 10:30:00 PM",
"Jun 18, 2014 11:30:00 PM",
"Jun 18, 2014 12:45:00 PM",
"Jun 18, 2014 2:45:00 PM",
"Jun 18, 2014 3:45:00 PM",
"Jun 18, 2014 4:45:00 PM",
"Jun 18, 2014 9:45:00 PM",
"Jun 18, 2014 11:45:00 PM"
)

例如,为什么“Jun 18,2014 12:15:00 AM”会在“Jun 18,2014 1:00:00 AM”之后显示?我只是没有得到这一部分!!

这是我的代码:

-(void)sortDates {
NSDateFormatter *dateFormatter;

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy HH:mm:ss a"];

NSLog(@"%@", xAxisDataWithDate);
NSArray *sortedTimes = [xAxisDataWithDate sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2)
{
    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];
    return [date1 compare:date2];
}];
NSLog(@"sorted dates = %@", sortedTimes);
}

我还写了一个示例程序,其中包含一个包含这些日期的子集,当阵列中存在凌晨1:00:00时,它似乎给我错误的结果(上午1:00:00应该存储在12之后: 00:00 AM):

-(void)sortDates {
NSDateFormatter *dateFormatter;

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy HH:mm:ss a"];

NSMutableArray *times = [[NSMutableArray alloc]initWithObjects:@"Jun 19, 2014 11:45:00 PM", @"Jun 18, 2014 1:00:00 AM", @"Jun 18, 2014 12:15:00 AM",@"Jun 18, 2014 12:00:00 AM", nil];
NSArray *sortedTimes = [times sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2)
{
    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];
    return [date1 compare:date2];
}];
NSLog(@"sorted dates = %@", sortedTimes);
}

输出:

sorted dates = (
"Jun 18, 2014 1:00:00 AM",
"Jun 18, 2014 12:00:00 AM",
"Jun 18, 2014 12:15:00 AM",
"Jun 19, 2014 11:45:00 PM"
)

我无法弄清楚为什么我会出现故障结果w.r.t NSMutableArray * xAxisDataWithDate和w.r.t NSMutableArray *次!在这方面请帮助我!赞赏。

1 个答案:

答案 0 :(得分:0)

您似乎格式错误,时间似乎为12小时格式,请将格式更改为MMM dd, yyyy hh:mm:ss a注意lowercase "h"

"Jun 18, 2014 12:00:00 AM",
"Jun 18, 2014 12:15:00 AM",
"Jun 18, 2014 1:00:00 AM",
"Jun 19, 2014 11:45:00 PM"

更重要的是,您应该在比较之前将字符串转换为日期,sortedArray…使用合并排序,因此您将字符串转换为日期n log n次而不是n