3

I have an NSMutableArray that contains dates in a string format. Here I need to sort that array in ascending order of dates. I used:

NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[dateArray sortedArrayUsingDescriptors:descriptors];

But it only sort the dates in terms of ascending order of day and month. Year is not considered.

For example, Array contains

03/09/2017, 03/06/2016, 01/06/2016,01/04/2016 and 03/01/2017.

After using the above lines of code, Array contains like,

01/04/2018, 01/06/2016, 03/01/2017, 03/06/2016, 03/09/2016
1
  • you date format mm/dd or dd/mm Commented Sep 3, 2016 at 8:49

5 Answers 5

8

You need to use sortedArrayUsingComparator to sort date with String array.

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSArray *sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
    NSDate *d1 = [df dateFromString: obj1];
    NSDate *d2 = [df dateFromString: obj2];
    return [d1 compare: d2];
}];

Note : Set format of date according to your date. It is hard to predict the date format from your example. That's why I have used MM/dd/yyyy, if your date is in the format dd/MM/yyyy then use that.

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

4 Comments

Sorry i have already use this code, but still i don't get the correct solution
Thanks for your response.
Can you give me the formate of date means it is dd/MM or MM/dd?
sorry it's my fault. And your answer is correct. Thank you very much
1

your code is correct but date format wrong.

try this code:

NSMutableArray *dateArray = [[NSMutableArray alloc] initWithArray: @[@"03/09/2017", @"03/06/2016", @"01/06/2016",@"01/04/2016", @"03/01/2017"]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSDate *date;
for (int i = 0; i < dateArray.count; i++) {
    [formatter setDateFormat:@"MM/dd/yyyy"];  // Please your date format set.
    date = [formatter dateFromString:[dateArray objectAtIndex:i]];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    [dateArray replaceObjectAtIndex:i withObject:[formatter stringFromDate:date]];
}
NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[dateArray sortedArrayUsingDescriptors:descriptors];

NSLog(@"Array: %@",reverseOrder);

Comments

1

Please try below code:

NSArray *strDateArray = [[NSArray alloc] initWithObjects:@"03/09/2017",@"03/06/2016", @"01/06/2016",@"01/04/2016",@"03/01/2017", nil];

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

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

    for (NSString *dateString in strDateArray) {
        NSDate *date = [formatter dateFromString:dateString];
        [dateArray addObject:date];
    }

    // sort array of dates
    [dateArray sortUsingComparator:^NSComparisonResult(NSDate *date1, NSDate *date2) {
        // return date1 compare date2 for ascending. And reverse the call for ascending.
        return [date1 compare:date2];
    }];

    NSLog(@"dateArray %@", dateArray);
    NSMutableArray *sortedDateArray = [[NSMutableArray alloc] init];

    for (NSDate *date in dateArray) {
        NSString *dateString = [formatter stringFromDate:date];
        [sortedDateArray addObject:dateString];
    }

    NSLog(@"sortedDateArray %@", sortedDateArray);

Comments

0

For Swift 4.0

let formater = DateFormatter()
    formater.dateFormat = "MM/dd/yyyy" //date formate should be according to your date formate
    let sortedArray=arrayToSort.sorted(){
        (obj1, obj2) in
        let date1=formater.date(from: obj1)
        let date2=formater.date(from: obj2)
        return date1!<date2!
    }

Comments

-1

//You can change the date format according to your code

NSDateFormatter *df = [[NSDateFormatter alloc] init];
                      [df setDateFormat:@"MM/dd/yyyy"];

NSArray *sortedArray = [YourArraysortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2)
{
    NSString *strDateObj1  = [NSString stringWithFormat:@"%@",[obj1 valueForKey:@"BirthDate"]];
    NSString *strDateObj2  = [NSString stringWithFormat:@"%@",[obj2 valueForKey:@"BirthDate"]];
    NSDate *d1 = [df dateFromString: strDateObj1];
    NSDate *d2 = [df dateFromString: strDateObj2];
    return [d1 compare: d2];
}];

NSLog(@"%@", sortedArray)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.