2

I have a dates in Array like below and i want to sort it.

 NSMutableArray *arrDates =[[NSMutableArray alloc]init];
[arrDates addObject:@"24/01/2010"];
[arrDates addObject:@"15/05/2014"];
[arrDates addObject:@"04/03/2011"];
[arrDates addObject:@"30/05/2013"];
[arrDates addObject:@"11/10/2012"];

I tried with below code but am not getting what should i pass exactly as a Key here.

NSSortDescriptor *sortDis = [[NSSortDescriptor alloc] initWithKey:????? ascending:TRUE];
[arr sortUsingDescriptors:[NSArray arrayWithObject:sortDis]];

I passed self but its sorting only based on date(day).

I want to display result as below

24/01/2010
04/03/2011
11/10/2012
30/05/2013
15/05/2014

and also i tried with below line

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

but no use. Please help.

2
  • 2
    they aren't dates. convert the strings to dates first. Commented Jun 4, 2013 at 11:36
  • 1
    like for (NSString *strDate in arrDates) { NSDateFormatter *formatter=[NSDateFormatter new]; [formatter setDateFormat:@"dd/MM/yyyy"]; NSDate *date=[formatter dateFromString:strDate]; [arr addObject:date]; } Commented Jun 4, 2013 at 11:38

4 Answers 4

8

Store your data using the appropriate type, which for dates is NSDate and then you can use:

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {
    return [date1 compare:date2];
}];

Which assumes array is an NSArray of NSDate objects.

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

7 Comments

+1 Most efficient answer :)
Also it has few lines of code.
+1 @trojanfoe, this is best answer. If i have 100 obj then will it slow in this condition?
This would not be slow even if you had "...OVER 10 000!!!!"
I am not getting Why the Date format is changing to 2009-10-01 this format?
|
4

Try

NSMutableArray *arrDates =[[NSMutableArray alloc]init];
[arrDates addObject:@"24/01/2010"];
[arrDates addObject:@"15/05/2014"];
[arrDates addObject:@"04/03/2011"];
[arrDates addObject:@"30/05/2013"];
[arrDates addObject:@"11/10/2012"];

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[arrDates sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];
    return [date1 compare:date2];
}];

6 Comments

Lots of conversion going on there, which cannot be efficient. What's wrong with converting the strings to dates before this? It will no doubt be useful whenever the array has to be presented or further processed.
@trojanfoe I just suggested a solution specific to the requirement. If I can I would never store dateString as such, but as NSDate instances. Thanks for your suggestion.
this is the right answer
@SamBudda Thanks for ur good words. But the other answer was accepted as it has another way of doing the same thing much more efficiently. If date is stored as string, conversion is needed where ever it is used. Mine is more specific to sorting current array of dateStrings :)
Problem with marked answer is that it is assuming the array has nsdate objects when clearly OP is storing strings. Just because the string looks like date doesn't mean iOS know they are dates. I have done this sort in the past and your way is correct as per OPs question
|
1

You need to add them as NSDate objects, for that use this:

NSString *dateString = @"01/02/2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

Once the array contains NSDates, it will be sorted correctly

Or if you dont want to store NSDates, when you are comparing it, convert them to dates and compare them

Comments

1

Store the dates as NSDate objects in an NS(Mutable)Array, then use [NSArray sortedArrayUsingSelector: or [NSMutableArray sortUsingSelector: and pass @selector(compare:) as the parameter. The -[NSDate compare:] method will order dates in ascending order for you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.