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.

Ganesh G
  • 1,991
  • 1
  • 24
  • 37
  • 2
    they aren't dates. convert the strings to dates first. – Wain Jun 04 '13 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]; } – Ganesh G Jun 04 '13 at 11:38

4 Answers4

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.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
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];
}];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • 1
    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 Jun 04 '13 at 11:39
  • 1
    @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. – Anupdas Jun 04 '13 at 11:40
  • this is the right answer – Sam B Jun 04 '13 at 12:49
  • @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 :) – Anupdas Jun 04 '13 at 12:54
  • 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 – Sam B Jun 04 '13 at 13:02
  • @SamBudda The conversion of string to date is very simple, so it's fair. – Anupdas Jun 04 '13 at 13:08
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

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
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.

Mudit Bajpai
  • 3,010
  • 19
  • 34