1

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.

4
  • 1
    Can you specify what large data is? Can you provide the data, and are you sure that it's correct? Commented Sep 20, 2013 at 9:13
  • @Sunny str1 and str2 hold the same card expiry date string coz you are using the same dictionary object a to get Card Expiry Date value. Commented Sep 20, 2013 at 9:14
  • large data its about the more than 1500 array(contains dictionary object) count. @NSAddict i am not sure ite perfect or not. Commented Sep 20, 2013 at 9:21
  • "trivial syntax error" category Commented Jun 4, 2014 at 11:24

1 Answer 1

2

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.

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

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.