在我的应用程序中,我有一个字符串数组,表示格式为 MM/dd/YYYY 的日期
下面是我用来排序这个数组的代码
NSDateFormatter *formatter =[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/YYYY"];
NSLog(@" arr %@",arr);
[arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSDate *date1=[formatter dateFromString:obj1];
NSDate *date2=[formatter dateFromString:obj2];
return [date1 compare:date2];
}];
NSLog(@" arr %@",arr);
下面是 nslog 的输出
2013-04-08 17:23:48.112 SEEMR[2792:c07] arr (
"02/18/2013",
"02/16/2013",
"02/14/2013",
"01/16/2013",
"02/13/2013",
"03/16/2013"
)
2013-04-08 17:24:07.662 SEEMR[2792:c07] arr (
"02/18/2013",
"02/16/2013",
"02/14/2013",
"01/16/2013",
"02/13/2013",
"03/16/2013"
)
但它没有按预期排序,所以请帮助我的同行