开发者

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.


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.


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];
                                               }];


Of course, I should use

initWithKey:ascending:selector:

and write my own comparison selector

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜