所以我有一个字符串数组(monthsArray),如下所示:
months array before :(
"November 2012",
"January 2013",
"September 2013",
"December 2012",
"August 2013"
)
我用它来排序:
NSDateFormatter * formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MMMM YYYY"];
monthsArray = [NSMutableArray arrayWithArray:[tempMonthsArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSTimeInterval firstTime=[[formatter dateFromString:(NSString*)a]timeIntervalSince1970];
NSTimeInterval secondTime=[[formatter dateFromString:(NSString*)b]timeIntervalSince1970];
return [[NSNumber numberWithDouble:firstTime]compare:[NSNumber numberWithDouble:secondTime]];
}]];
在我对其进行排序后,它看起来像这样:
months array after :(
"November 2012",
"December 2012",
"January 2013",
"September 2013",
"August 2013"
)
正如您在“排序”数组之前和之后看到的那样......它们没有正确排列。为什么不?我个人看不出代码有什么问题。