Sunny Shah
Sunny Shah

Reputation: 13020

sortedArrayUsingComparator not working for array in IOS?

hello i want to sort the array with oldest to latest date. it works perfect for the small data. but when the data is large not getting the perfect soreted array.

here is my code

  -(NSMutableArray *)sortArrayOldestTonewest:(NSMutableArray *)arr{

    NSArray *sortedArray;

    NSMutableArray *arrResponse=[NSMutableArray arrayWithArray:arr];

    sortedArray = [arrResponse sortedArrayUsingComparator:^(id a,id b) {
        NSString *str1 = [a valueForKey:@"CardExpiryDate"];
        NSString *str2 = [b valueForKey:@"CardExpiryDate"];

        NSDate *date1=[Utility dateFromString:str1 withFormat:@"dd/mm/yyyy"];
        NSDate *date2=[Utility dateFromString:str2 withFormat:@"dd/mm/yyyy"];

        return [date1 compare:date2];

    }];

    NSMutableArray  *arrResult=[[NSMutableArray alloc]initWithArray:sortedArray];
    return arrResult
    ;

}

what i am doing wrong. please help.

Upvotes: 1

Views: 676

Answers (1)

Martin R
Martin R

Reputation: 539975

Your date format is wrong, it should be @"dd/MM/yyyy".

"MM" is for month, "mm" is for minutes. It must be pure coincidence that your results were correct for small datasets.

Upvotes: 2

Related Questions