How to sort the Array that contains date in string

2019-03-02 07:42发布

问题:

This question already has an answer here:

  • NSDateFormatter return incorrect date from string 3 answers

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

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

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.

回答1:

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 is a point in time. It does not (and never does) contain any formatting information.

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.

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:

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:

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.