如何按字母顺序对字符串中包含日期的数组进行排序? [重复]

时间:2022-05-10 07:20:45

This question already has an answer here:

这个问题在这里已有答案:

I want to sort the array that contains date string in descending order, I have tried SO answers, but I am getting wrong output.

我想按降序排序包含日期字符串的数组,我已经尝试了SO答案,但我的输出错误。

Please see my code as below:

请参阅我的代码如下:

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 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" )

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“)

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.

我不知道为什么“12”会在月份出现,而且日期格式也不像我指定的格式。

3 个解决方案

#1


7  

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);

EDIT TO PUT INTO STRING ARRAY

编辑投入阵营

NSMutableArray *correctOrderStringArray = [NSMutableArray array];

for (NSDate *date in tempArray) {
    NSString *dateString = [formatter stringFromDate:date];
    [correctOrderStringArray addObject:dateString];
}

NSLog(@"%@", correctOrderStringArray);

Also, read up more about NSDate, NSString and NSDateFormatter. Especially how they interact with each other.

另外,请阅读有关NSDate,NSString和NSDateFormatter的更多信息。特别是他们如何互相交流。

NSDate is a point in time. It does not (and never does) contain any formatting information.

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.

NSDateFormatter不保存有关特定日期(即时间点)的任何信息。它用于获取日期并根据NSDate和您提供的格式生成字符串。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

#2


0  

Try

[formatter setDateFormat:@"MM/dd/yyyy"]

Instead of what you have in Line 3. This should give you the date format you are expecting. With regards to sorting, I am a little confused. You have no code that sorts. Are you trying to sort or just format the date string?

而不是第3行中的内容。这应该为您提供您期望的日期格式。关于排序,我有点困惑。您没有可以排序的代码。您是要尝试排序还是只是格式化日期字符串?

#3


0  

You have to build yourself a suitable comparator:

你必须建立一个合适的比较器:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:enUSPOSIXLocale];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:@"MM/dd/yyyy"];

NSComparator myDateComparator = ^NSComparisonResult(id obj1,
                                                    id obj2) {
    NSDate *date1 = [formatter dateFromString:obj1];
    NSDate *date2 = [formatter dateFromString:obj2];

    return [date2 compare:date1]; // sort in descending order
};

then simply sort the array by doing:

然后只需按以下方式对数组进行排序

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"];

NSArray *sortedArray = [dateArray sortedArrayUsingComparator:myDateComparator];

NSLog(@"Sorted array is %@", sortedArray);

This sorts the strings (and not any modified array). You could also use string manipulation and convert m/d/y to y-m-d, but i guess for small data sets this approach would be fine.

这会对字符串进行排序(而不是任何已修改的数组)。你也可以使用字符串操作并将m / d / y转换为y-m-d,但我想对于小数据集这种方法会没问题。

#1


7  

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);

EDIT TO PUT INTO STRING ARRAY

编辑投入阵营

NSMutableArray *correctOrderStringArray = [NSMutableArray array];

for (NSDate *date in tempArray) {
    NSString *dateString = [formatter stringFromDate:date];
    [correctOrderStringArray addObject:dateString];
}

NSLog(@"%@", correctOrderStringArray);

Also, read up more about NSDate, NSString and NSDateFormatter. Especially how they interact with each other.

另外,请阅读有关NSDate,NSString和NSDateFormatter的更多信息。特别是他们如何互相交流。

NSDate is a point in time. It does not (and never does) contain any formatting information.

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.

NSDateFormatter不保存有关特定日期(即时间点)的任何信息。它用于获取日期并根据NSDate和您提供的格式生成字符串。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

#2


0  

Try

[formatter setDateFormat:@"MM/dd/yyyy"]

Instead of what you have in Line 3. This should give you the date format you are expecting. With regards to sorting, I am a little confused. You have no code that sorts. Are you trying to sort or just format the date string?

而不是第3行中的内容。这应该为您提供您期望的日期格式。关于排序,我有点困惑。您没有可以排序的代码。您是要尝试排序还是只是格式化日期字符串?

#3


0  

You have to build yourself a suitable comparator:

你必须建立一个合适的比较器:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:enUSPOSIXLocale];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setDateFormat:@"MM/dd/yyyy"];

NSComparator myDateComparator = ^NSComparisonResult(id obj1,
                                                    id obj2) {
    NSDate *date1 = [formatter dateFromString:obj1];
    NSDate *date2 = [formatter dateFromString:obj2];

    return [date2 compare:date1]; // sort in descending order
};

then simply sort the array by doing:

然后只需按以下方式对数组进行排序

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"];

NSArray *sortedArray = [dateArray sortedArrayUsingComparator:myDateComparator];

NSLog(@"Sorted array is %@", sortedArray);

This sorts the strings (and not any modified array). You could also use string manipulation and convert m/d/y to y-m-d, but i guess for small data sets this approach would be fine.

这会对字符串进行排序(而不是任何已修改的数组)。你也可以使用字符串操作并将m / d / y转换为y-m-d,但我想对于小数据集这种方法会没问题。