0

I have an array that contains date strings like

"9/15/2011 12:00:00 AM", "10/15/2011 12:00:00 AM", "11/16/2011 12:00:00 AM"

How can I sort this array in descending order? The order should be, sort by year first, then, month, then date and then time.

Please share your ideas. Thank you

4
  • 1
    The main thing is that you can not sort NSDates when they are in the format of NSStrings. Then, the solution is here! Commented Sep 23, 2011 at 4:34
  • thanks for the reply. The I got the date values from a web service, so how can I convert this string date values into NSDate format? Is it possible? Commented Sep 23, 2011 at 4:45
  • I have got the code to change to NSdate, but the string format that I am getting from the web service is like "11/16/2011 12:00:00 AM". How can i convert this one? Commented Sep 23, 2011 at 4:58
  • Yes. It is possible. You should use NSDateFormatter. I've posted an answer now. Commented Sep 23, 2011 at 4:59

5 Answers 5

2

Sorting NSDates while they are in NSString format is almost not possible, except if they are in the format "YYYYMMddHHmmss" which you can sort as sorting strings. Refer @Daniel R Hicks answer.

Convert the string into NSDate while you parse and add the dates from web service.

NSString *dateStr = ...
NSDateFormatter *df = ;[NSDateFormatter alloc] init];
[df setDateFormat:@"M/dd/YYYY hh:mm:ss a"];

[datesArray addObject:[df dateFromString:dateStr]];

And the sorting,

[datesArray sortUsingSelector:@selector(compare:)];

And the original answer is here!

Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much for your answer. One more, I got the answer like 2011-11-15 18:30:00 +0000, but the input was 11/16/2011 12:00:00 AM. There is a difference in the date. I think that is because of the time format. Is there anyway to get the time in 12hr format?
You don't have to worry about the format the iOS displays the date. It always displays the date with respect to the time zone. When you convert the date to string you will get the correct value. Don't worry about it.
If you don't mind, would you please tell, what should be the function that come under the 'compare:' action? How can I compare the sorted date array with the system date? Thanks
2

try belowed listed code. it will give sort date in ascending order.

NSMutableArray *arraydate=[[NSMutableArray alloc] init];
[arraydate addObject:@"22/7/2010"];
[arraydate addObject:@"1/1/1988"];

[arraydate addObject:@"22/7/1966"];

[arraydate addObject:@"22/7/2000"];

[arraydate addObject:@"1/7/2010"];

[arraydate sortUsingSelector:@selector(compare:)];

NSLog(@"array=%@",[arraydate description]);

Comments

1

If they're already strings you can either convert them to NSDates (and hence to NSTimeIntervals) and sort those, or effectively rearrange the digits to YYMMDDHHmmss, where HH has been converted to 00-23 representation.

Either can be done by using a sortUsingFunction call.

Comments

0
static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |NSWidthInsensitiveSearch | NSForcedOrderingSearch;

    NSLocale *currentLocale = [NSLocale currentLocale];

    NSArray *sortArray = [iArray sortedArrayUsingComparator:^(id string1, id string2) {

        NSRange string1Range = NSMakeRange(0, [string1 length]);

        //if([string2 isEqualToString:@"#"])
            //return -1;

        return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
    }];

    return sortArray;

Comments

0

There is a method for comparing two dates.

 int interval = [date1 compare:date2];

        if (interval == 1)
        {
            NSLog(@"Right hand side date is earlier");
        }
        else if (interval == -1)
        {
            NSLog(@"Left hand side date is earlier");
        }

        else 
        {
            NSLog(@"Both Dates are equal");
        }

you can use this method but make sure that your date1 and date2 are NSDate objects and after comparing them you can use bubble sorting method along with it to sort the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.