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

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Mithun
  • 459
  • 1
  • 10
  • 32
  • 1
    The main thing is that you can not sort NSDates when they are in the format of NSStrings. Then, [the solution is here!](http://stackoverflow.com/questions/1132806/sort-nsarray-of-date-strings-or-objects/1134126#1134126) – EmptyStack Sep 23 '11 at 04: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? – Mithun Sep 23 '11 at 04: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? – Mithun Sep 23 '11 at 04:58
  • Yes. It is possible. You should use *NSDateFormatter*. I've posted an answer now. – EmptyStack Sep 23 '11 at 04:59

5 Answers5

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!

Community
  • 1
  • 1
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • 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? – Mithun Sep 23 '11 at 05:15
  • 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. – EmptyStack Sep 23 '11 at 05:17
  • 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 – Mithun Sep 23 '11 at 05:57
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]);
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
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.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
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;
Anand
  • 2,086
  • 2
  • 26
  • 44
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.

Dip Dhingani
  • 2,499
  • 2
  • 20
  • 18