-3

I'm trying to sort an NSArray of objects based on a date property. The order I'm looking for is the most recent date first, ending with the earliest date.

e.g 05/15/2014 05/12/2014 02/20/2014 25/12/2013 10/11/2013

I've attempted a number of different approaches seen in the accepted answer in "This" thread.

But my ordering is off for example

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateToPay" ascending:YES];
            NSArray *orderedArray = [cashFlowItemsArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            self.cashFlowItems = orderedArray;

is returning:
02/20/2014
05/12/2014
05/15/2014
10/11/2013
12/25/2013

So the year is ordered correctly, the months and days are wrong

Any help is much appreciated.

3 Answers3

0

Possible duplicate: Sort NSArray of date strings or objects

He basically states (assuming your array is filled with NSDate objects) to use the compare: selector for dates. So do this:

NSArray *orderedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];

He states:

This is simpler than creating an NSSortDescriptor, and much simpler than writing your own comparison function. (NSDate objects know how to compare themselves to each other at least as efficiently as we could hope to accomplish with custom code.)

Community
  • 1
  • 1
Nate Lee
  • 2,842
  • 1
  • 24
  • 30
0

check this , i have tested your code

 NSArray *stringDateArray = @[@"05/15/2014",@"05/12/2014",@"02/20/2014",@"05/12/2013",@"10/11/2013"];

 NSMutableArray *dateArray = [NSMutableArray arrayWithCapacity:[stringDateArray count]];

for (NSString *dateString in stringDateArray) {

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

    [dateArray addObject:@{@"date":[dateFormatter dateFromString:dateString]}];
}

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *orderedArray = [dateArray sortedArrayUsingDescriptors:@[sortDescriptor]];

NSLog(@"ordered array--%@",orderedArray);

ordered array--( { date = "2013-05-11 18:30:00 +0000"; }, { date = "2013-10-10 18:30:00 +0000"; }, { date = "2014-02-19 18:30:00 +0000"; }, { date = "2014-05-11 18:30:00 +0000"; }, { date = "2014-05-14 18:30:00 +0000"; } )

with compare selector

NSArray *stringDateArray = @[@"05/15/2014",@"05/12/2014",@"02/20/2014",@"05/12/2013",@"10/11/2013"];

NSMutableArray *dateArray = [NSMutableArray arrayWithCapacity:[stringDateArray count]];

for (NSString *dateString in stringDateArray) {

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

    [dateArray addObject:[dateFormatter dateFromString:dateString]];
}

NSArray *orderedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"ordered array--%@",orderedArray);

ordered array--( "2013-05-11 18:30:00 +0000", "2013-10-10 18:30:00 +0000", "2014-02-19 18:30:00 +0000", "2014-05-11 18:30:00 +0000", "2014-05-14 18:30:00 +0000" )

Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • My array is an array of objects, each object has a date property. I'm trying to order of that property – user3653821 May 19 '14 at 19:05
  • please correct me here , you have a array with date property ? means inside of your array , you have a dictionary with key @"date" & object @"NSDate" rihgt? – Pawan Rai May 19 '14 at 19:07
  • I have an array, the array is filled with objects of the same type, each object has a date property, the data type of the date property is an NSdate – user3653821 May 19 '14 at 19:10
  • will you show me the code how you fill your cashFlowItemsArray ? – Pawan Rai May 19 '14 at 19:11
  • The data is being returned from an API call, within the reponse i'm parsing Json data into a cashflowitem object, I've then poulated the array with these cashflowitem objects. – user3653821 May 19 '14 at 19:21
  • can you post the json response here? – Pawan Rai May 19 '14 at 19:27
  • jsonItems: { amount = 50; checkEnteredManually = 0; checkNumber = *********; dateEntered = "10/22/2013"; dateToPay = "10/23/2013"; description = "Visa Platinum"; func = billPayGetOutstanding; matches = "*****************"; paymentAmount = 50; paymentID = 618; pmtkey = "******************"; recurrence = 0; status = Outstanding; vendorID = 23; vendorName = ********; } i've covered any sensitive data with *, but that basically the response – user3653821 May 19 '14 at 19:34
  • dateToPay contain string object , right ? i can see that when you are sorting it , you get object sorted by sring. you need to convert them into nsdate as i have done with a datefomatter , after that you can sort it. – Pawan Rai May 19 '14 at 19:39
  • So in my cashflowitem i've created a setter for the date, it changes the string to date upon creation! and my @(selector)compare is now doing the job. Thanks Dude! – user3653821 May 19 '14 at 19:50
0

Make your array into a NSDate array with this code in a for:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MM/dd/yyyy"]; NSDate *myDate = [df dateFromString: @"02/20/2014"];

Then:

[dateArray sortUsingSelector: @selector(compare:)];

Mepla
  • 438
  • 4
  • 16