How to sort the Array that contains date in strings in descending order? [duplicate]

≯℡__Kan透↙ 提交于 2019-12-02 04:04:17

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

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?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!