Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSSortDescriptor and nil values

I'm using an NSSortDescriptor to sort NSDate objects in an ascending order. However, I need nil dates to be at the bottom of the list, whereas at the moment they come at the top.

like image 361
Daniel Wood Avatar asked Jan 07 '10 15:01

Daniel Wood


2 Answers

In the end I have decided that having nil values is not a good idea and if I want what were nil value to appear at the bottom of the ascending list I should set the dates to [NSDate distantFuture] and them check for this before displaying them. It turns out this makes more semantic sense in within the applications as well.

like image 66
Daniel Wood Avatar answered Sep 24 '22 01:09

Daniel Wood


Looks like you already have a solution, but for anyone else looking to still use nil NSDates, and have them at the bottom, I use the following code where I sort descending, and then reverse the order of the objects sent to the compare.

_sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dueDate"
                                                ascending:NO
                                               comparator:^NSComparisonResult(id obj1, id obj2)  {
                                                   return [obj2 compare:obj1];
                                               }];
like image 34
Tim Ritchey Avatar answered Sep 23 '22 01:09

Tim Ritchey