简体   繁体   English

核心数据按日期升序然后按 nil 排序

[英]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.我有一个表格,我正在按dueDate 升序对行进行排序。 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;示例:2011 年 1 月 1 日; Feb 1, 2011; 2011 年 2 月 1 日; June 1, 2011; 2011 年 6 月 1 日; nil;零; nil;零; nil

However, the default sorting puts the nil values first, as expected: nil;但是,默认排序将 nil 值放在首位,正如预期的那样: nil; nil;零; nil;零; Jan 1, 2011; 2011年1月1日; Feb 1, 2011; 2011 年 2 月 1 日; June 1, 2011 2011 年 6 月 1 日

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.我尝试在排序描述符中使用 NSComparisonResult 块,除了在实际启动它时遇到一些困难(可能是由于其他地方的一些逻辑 - 我在同一视图中切换不同的排序),我无法让它实际比较 null带有日期的键值。 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.我认为这是有道理的,因为其@"dueDate" 键为 null 的托管对象或多或少会被那些实际有要比较的对象的比较跳过。

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.在这一点上,我相信我唯一的选择是解决这个问题并将“无截止日期”记录设置为远在未来的日期,例如 3000 年 1 月 1 日。可能会显示 3000 年 1 月 1 日的任何地方,隐藏该值,就好像它实际上是 null。

Does this sound right and reasonable?这听起来正确和合理吗?

Thanks!谢谢!

I solved this problem as I suggested in the above comment: [NSDate distantFuture].正如我在上面的评论中建议的那样,我解决了这个问题:[NSDate distinctFuture]。 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.在我通常使用 nil 值的地方,我将 dat 设置为 distinctFuture 并在应用程序中根据该值进行过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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