如何按字母顺序对字符串中包含日期的数组进行排序? [英] How to sort the Array that contains date in strings in descending order?
问题描述
我想按降序对包含日期字符串的数组进行排序,我已尝试过SO答案,但输出错误。
I want to sort the array that contains date string in descending order, I have tried SO answers, but I am getting wrong output.
请参阅下面的代码:
NSArray *dateArray=[NSArray arrayWithObjects:@"01/12/2012",@"01/26/2011",@"02/09/2005",@"02/24/2006",@"03/19/2007",@"07/14/2011",@"08/17/2007",@"10/04/2007",@"10/31/2006",@"12/05/2012",@"12/06/2006",@"12/23/2008",nil];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/DD/YYYY"];
NSMutableArray *tempArr=[[NSMutableArray alloc]init];
for (int i=0; i<[dateArray count]; i++) {
NSString *dt=[dateArray objectAtIndex:i];
NSDate *newDt=[formatter dateFromString:dt];
[tempArr addObject:newDt];
}
NSLog(@"tempArr is %@",tempArr);
我得到如下控制台输出:
I am getting Console output like as below:
2013-08-27 15:29:50.418样本[3688:c07] tempArr是(
2011-12-24 18:30:00 +0000,
2010-12-18 18:30:00 +0000,
2004-12-18 18:30:00 +0000,
2005-12-24 18:30: 00 +0000,
2006-12-23 18:30:00 +0000,
2010-12-18 18:30:00 +0000,
2006- 12-23 18:30:00 +0000,
2006-12-23 18:30:00 +0000,
2005-12-24 18:30:00 +0000,
2011-12-24 18:30:00 +0000,
2005-12-24 18:30:00 +0000,
2007-12-22 18: 30:00 +0000
)
2013-08-27 15:29:50.418 sample[3688:c07] tempArr is ( "2011-12-24 18:30:00 +0000", "2010-12-18 18:30:00 +0000", "2004-12-18 18:30:00 +0000", "2005-12-24 18:30:00 +0000", "2006-12-23 18:30:00 +0000", "2010-12-18 18:30:00 +0000", "2006-12-23 18:30:00 +0000", "2006-12-23 18:30:00 +0000", "2005-12-24 18:30:00 +0000", "2011-12-24 18:30:00 +0000", "2005-12-24 18:30:00 +0000", "2007-12-22 18:30:00 +0000" )
我不知道为什么12将在月份来临还有日期格式不像我指定的格式。
I don't know why "12" is coming in the place of Month and also the date format not coming like the format I specified.
推荐答案
嗯,首先,你实际上并没有对任何东西进行排序在你的代码中...
Well, first off, you're not actually sorting anything in your code...
你应该做这样的事情......
You should do something like this...
// create the string array
NSArray *dateArray = @[@"01/12/2012", @"01/26/2011", @"02/09/2005", @"02/24/2006", @"03/19/2007", @"07/14/2011", @"08/17/2007", @"10/04/2007", @"10/31/2006", @"12/05/2012", @"12/06/2006", @"12/23/2008"];
// create the date formatter with the correct format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSMutableArray *tempArray = [NSMutableArray array];
// fast enumeration of the array
for (NSString *dateString in dateArray) {
NSDate *date = [formatter dateFromString:dateString];
[tempArray addObject:date];
}
// sort the array of dates
[tempArray sortUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {
// return date2 compare date1 for descending. Or reverse the call for ascending.
return [date2 compare:date1];
}];
NSLog(@"%@", tempArray);
编辑进入串联阵列
NSMutableArray *correctOrderStringArray = [NSMutableArray array];
for (NSDate *date in tempArray) {
NSString *dateString = [formatter stringFromDate:date];
[correctOrderStringArray addObject:dateString];
}
NSLog(@"%@", correctOrderStringArray);
另外,请阅读更多关于 NSDate
的信息, NSString
和 NSDateFormatter
。特别是他们如何互相交流。
Also, read up more about NSDate
, NSString
and NSDateFormatter
. Especially how they interact with each other.
NSDate
是一个时间点。它不(并且从不)包含任何格式信息。
NSDate
is a point in time. It does not (and never does) contain any formatting information.
NSDateFormatter
不包含任何有关a的信息具体日期(即时间点)。它用于获取日期并根据您提供的 NSDate
和格式
生成字符串。
NSDateFormatter
doesn't hold any information about a specific date (i.e. point in time). It is used to take a date and produce a string based on the NSDate
and the format
that you give it.
这篇关于如何按字母顺序对字符串中包含日期的数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!