-1

I have an NSArray with objects that have a property called "dueDate" every object has a date assigned which is a NSString, but there are objects where the dueDate is "-" (meaning the user didn't add a specific date), I must reorder the NSArray so the objects with the "-" dueDate are ALWAYS at the bottom, and the ones with a specific date are ordered ascending and at the top of the NSArray.

here is the NSArray and an example of the data of the objects:

enter image description here

enter image description here

enter image description here

  • 1
    So? Review the documentation for NSArray/NSMutableArray. There are several `sortedArrayUsing...` methods that you can use to produce any sort sequence you want. – Hot Licks Aug 04 '14 at 16:49
  • 2
    Why isn't `dueDate` an `NSDate` instead of an `NSString`? Would make a lot of things much easier. – rmaddy Aug 04 '14 at 16:55
  • You can always convert it to NSDate, but one of your main issues is the date format in the string: It could be 08/03/2014 or 8/3/2014, (or really any other format...8/03/2014 or Aug 3,2014) and since it's a string, you'll have to parse it first. Once you can tell which date really is bigger than another, than you can use a custom comparator as below to do the actual sorting. – Owen Hartnett Aug 04 '14 at 17:31

1 Answers1

0

You can use sortedArrayUsingComparator:. You would do something like this

NSArray *sortedByDueDate = [toBeSortedByDueAte sortedArrayUsingComparator:^NSComparisonResult(id lhs, id rhs) {
    SavedTask *lhs_task = (SavedTask *)lhs;
    SavedTask *rhs_task = (SavedTask *)rhs;
    NSString *lhs_dueDate = lhs_task.dueDate;
    NSString *rhs_dudDate = rhs_task.dueDate;
    NSComparisonResult const result = /* Your code for comparing the strings here */
    return result
}];

In production code you should verify that lhs and rhs are of the expected type.

Lev Landau
  • 788
  • 3
  • 16