Search code examples
ios6

sorting array with date in string without comparator


everyone I am in another mess. I need to sort an array with date in strings and names corresponding to their birthday (date).And then view it in table view. Is there any way?


Solution

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

    sortedDataSource = [BdayArr sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
        NSDate *d1 = [formatter dateFromString:obj1[kKeyDOB]];
        NSDate *d2 = [formatter dateFromString:obj2[kKeyDOB]];
    
        return [d1 compare:d2]; // ascending order
        //return [d2 compare:d1]; // descending order
    }];
    
    NSLog(@"%@",sortedDataSource);
    }