简体   繁体   中英

Core Data sort by Date ascending then by nil

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM