Search code examples
iosobjective-cnsdateformatter

Sort array of dates with date month and year in objective c


I want to sort an array with a date but the date has only sort with date and month, and not sorting with year my code:

NSArray *sortedArray = [arrClassSehedule sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    NSDate *d1 = [appSharedData.CommanFormatForConditional dateFromString: [obj1 objectForKey:@"scheduleStart"]];
    NSDate *d2 = [appSharedData.CommanFormatForConditional dateFromString: [obj1 objectForKey:@"scheduleStart"]];
    return [d1 compare: d2];
}];

It's not sorting properly by date.


Solution

  • It seems like a typo.

    Please replace the code with

     NSDate *d2 = [appSharedData.CommanFormatForConditional dateFromString: [obj2 objectForKey:@"scheduleStart"]];