2

I have an Array in which contains a list of dates represented as strings:

 NSMutableArray *objArray=[[NSMutableArray alloc]init];

 [objArray addObject:@"18-01-2013 2:51"];
 [objArray addObject:@"16-01-2013 5:31"];
 [objArray addObject:@"15-01-2013 3:51"];
 [objArray addObject:@"17-01-2013 4:41"];
 [objArray addObject:@"03-02-2013 3:21"];
 [objArray addObject:@"05-01-2013 3:01"];

please tell me how to arrange this array in ascending order by using dates.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
Ravi
  • 888
  • 6
  • 24
  • have a look at this question it's help me a lot to you http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – Hiren Feb 06 '13 at 05:19
  • Convert your strings to actual `NSDate` objects first, and then sort with a sort descripter. – dreamlax Feb 06 '13 at 05:32

6 Answers6

3

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"dd-MM-yyyy hh:mm"];

    NSComparator compareDates = ^(id string1, id string2)
    {
        NSDate *date1 = [formatter dateFromString:string1];
        NSDate *date2 = [formatter dateFromString:string2];

        return [date1 compare:date2];
    };

    NSSortDescriptor * sortDesc = [[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO comparator:compareDates]autorelease];

    [objArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

By using this code i got exact output, thank you for all

Ravi
  • 888
  • 6
  • 24
3

we can do another way to get sorting array with dates

    NSArray *objArr = [[NSArray alloc]initWithObjects:@"2003/08/02 03:00 ",@"2001/02/04 04:00 ",@"2001/02/04 05:00 ",@"2010/05/01 08:00 ",@"2002/12/02 02:00 ",@"2012/11/05 02:00 ",@"2013/10/01 12:00 ", nil];

    NSArray *sortedArray = [objArr sortedArrayUsingComparator:^(id firstObject, id secondObject) {
          return [(NSDate * )firstObject compare: (NSDate * ) secondObject];
     }];

NSLog(@"sorteed Array %@",sortedArray);
Ravi
  • 888
  • 6
  • 24
0
NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES];
[livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]];
 [descLastname release];
videoparsing = [livevideoparsingarray copy];

livevideoparsingarray is my array which I have sort & active is my tag which is in array which I have sort. You change with your requirements.

Vishal
  • 8,246
  • 6
  • 37
  • 52
0

You can do this by using NSSortDescriptor

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
return [objArray sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
0
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *str1 = @"18-01-2013";
    NSString *str2 = @"16-01-2013";
    NSString *str3 = @"15-01-2013";
    NSString *str4 = @"17-01-2013";
    NSString *str5 = @"03-02-2013";
    NSString *str5 = @"05-01-2013";

    NSArray *arr = [NSArray arrayWithObjects:str1, str2, str3,str4,str5 nil];
    arr = [arr sortedArrayUsingFunction:dateSort context:nil];
    NSLog(@"New dates %@",arr);

}
//The date sort function
NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-yyyy"];

    NSDate *d1 = [formatter dateFromString:s1];
    NSDate *d2 = [formatter dateFromString:s2];

    return [d1 compare:d2]; // ascending order
//    return [d2 compare:d1]; // descending order
}
Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • It would be better to convert the objects in array to dates *before* sorting rather than converting them to dates during a sort. – dreamlax Feb 06 '13 at 05:42
0

Try this

     NSMutableArray *objArray;
     objArray=[[NSMutableArray alloc] init];
     [objArray addObject:@"18-01-2013 2:51"];
     [objArray addObject:@"16-01-2013 5:31"];
     [objArray addObject:@"15-01-2013 3:51"];
     [objArray addObject:@"17-01-2013 4:41"];
     [objArray addObject:@"03-02-2013 3:21"];
     [objArray addObject:@"05-01-2013 3:01"]; 

     NSSortDescriptor *sort_Date=[[NSSortDescriptor alloc] initWithKey:@"self"ascending:NO];
  NSLog(@"%@",[[objArray sortedArrayUsingDescriptors:@[sort_Date]] description]);

I got below response

2013-02-06 11:23:35.100[1135:c07] 
  (
"18-01-2013 2:51",
"17-01-2013 4:41",
"16-01-2013 5:31",
"15-01-2013 3:51",
"05-01-2013 3:01",
"03-02-2013 3:21"

)

Vishwa816
  • 321
  • 1
  • 4
  • 7
  • but in this order we should get top index 03-02-2013 3:21 date, i think it is compare only first term (i mean date) – Ravi Feb 06 '13 at 06:02