开发者

Core Data sort by Date ascending then by nil

开发者 https://www.devze.com 2023-03-11 06:15 出处:网络
I think I already know the answer, but I\'m going to ask the question anyway... I have a table where I\'m sorting rows by dueDate ascending. Some records have due dates, some don\'t (nil). I prefer t

I think I already know the answer, but I'm going to ask the question anyway...

I have a table where I'm sorting rows by dueDate ascending. Some records have due dates, some don't (nil). I prefer to show all the rows with due dates before those that don't. Example: Jan 1, 2011; Feb 1, 2011; June 1, 2011; nil; nil; nil

However, the default sorting puts the nil values first, as expected: nil; nil; nil; Jan 1, 2011; Feb 1, 2011; June 1, 2011

I tried using an NSComparisonResult block in a sort descriptor and besides having some difficulty actually getting it to fire (probably due to some logic elsewhere - I am toggling different sorts in the same view), I can'开发者_运维百科t get it to actually compare the null key values to the ones with dates. I think this makes sense, as the managed objects whose @"dueDate" keys are null are more or less skipped over by the comparison for those where there are actually objects to compare.

At this point, I believe my only option is to hack through this and make the "no due date" records set to have a date far into the future, eg Jan 1, 3000. Anywhere that 1/1/3000 date might be shown, hide the value as if it were actually null.

Does this sound right and reasonable?

Thanks!


I solved this problem as I suggested in the above comment: [NSDate distantFuture]. So far it has been working in my app consistently, and I have no reason to believe it will stop working this way in the future... or even distant future.

In places where I would normally use a nil value, I set the dat to distantFuture and filtered by that value as appropriate in the app.

0

精彩评论

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