1

I have an NSArray containing date/time NSStrings in the following format:

2/2/2011 2:46:39 PM
2/4/2011 11:59:47 AM
…

where the date is represented as month/day/year.

How do I sort this NSArray making sure the newest date/times are at the top?

2
  • NSString that is being returned from a web service. Commented May 11, 2011 at 3:58
  • The first field is the month. I'm sorry I should have mentioned that. Commented May 11, 2011 at 4:03

3 Answers 3

3

When you’re dealing with dates, use NSDate instead of NSString. Also, it’s important to consider the time zone — does the Web service provide dates in UTC or some other time zone?

You should first convert your array of strings into an array of dates. Otherwise, you’d be converting a string to a date whenever it is used for comparison, and there will be more comparisons than the number of strings.

For example:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];

NSMutableArray *dateArray = [NSMutableArray array];
for (NSString *dateString in array) {
    NSDate *date = [formatter dateFromString:dateString];
    if (date) [dateArray addObject:date];
    // If the date is nil, the string wasn't a valid date.
    // You could add some error reporting in that case.
}

This converts array, an array of NSStrings, to dateArray, a mutable array of NSDates. The date formatter uses the system time zone. If you want to use UTC as the time zone:

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

Having done that, sorting the array is trivial:

[dateArray sortUsingSelector:@selector(compare:)];
Sign up to request clarification or add additional context in comments.

Comments

0

Use method compare to compare two dates,

Sample NSDate comparision,

NSDate *dateOne = [NSDate dateWithString:@"2008-12-04 03:00:00 +0900"];
NSDate *dateTwo = [NSDate dateWithString:@"2008-12-04 04:00:00 +0900"];

switch ([dateOne compare:dateTwo]){
case NSOrderedAscending:
NSLog(@”NSOrderedAscending”);
break;
case NSOrderedSame:
NSLog(@”NSOrderedSame”);
break;
case NSOrderedDescending:
NSLog(@”NSOrderedDescending”);
break;
}

Use you own logic to sort

Comments

0

use this

    NSMutableArray *mutArr=[yourArray mutableCopy];//convert your NSArray into mutable array
    NSDateFormatter *df=[[[NSDateFormatter alloc] init] autorealese];
    [df setDateFormat:@"MM/DD/YYYY hh:mm:ss a"];
    NSDate *compareDate;
NSInteger index;
for(int i=0;i<[mutArray count];i++)
{
    index=i;
    compareDate=[df dateFromString:[mutArray objectAtIndex:i]];
    NSDate *compareDateSecond;
    for(int j=i+1;j<counter;j++)
    {
        compareDateSecond=[df dateFromString:[mutArr objectAtIndex:j]];
        NSComparisonResult result = [compareDate compare:compareDateSecond];
       if(result == NSOrderedAscending)
       {
           compareDate=compareDateSecond;
           index=j;
       }
    }
    if(i!=index)
      [mutArr exchangeObjectAtIndex:i withObjectAtIndex:index];
}
    }

NSLog(@"%@",mutArr);

4 Comments

Please give some extra effort in typing a comment why this down vote.
I didn't vote your answer down, but it looks like you're doing something like bubble sort, but since it is only doing one pass, it will not work. Also, bubble sort is very slow. (Note, as is, your code won't compile because dateTwo isn't declared.)
Even after your edit, the sorting code still doesn’t work. In general, you cannot sort an array in linear time.
Thanks @Bavarious and @ThomasW, now i think it definitely work.in hurry i had done a mistake.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.