iPhone:如何使用 NSDate 对日期进行排序?

发布于 2024-08-22 18:06:25 字数 46 浏览 6 评论 0原文

我必须根据 NSDates 对对象进行排序。按 NSDate 排序有哪些选项?

I have to sort objects based on NSDates. What are the options for sorting by NSDate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-08-29 18:06:25

一般来说,您使用 NSSortDescriptor 定义数组或核心数据等集合的排序。您可以从集合对象本身调用排序。

如果你想自己直接排序,NSDate 本身就有比较函数。

In general you use a NSSortDescriptor to define the sort of collections like arrays or Core Data. You call the sort from the collection object itself.

NSDate itself has comparison functions if you want to do the sort directly yourself.

旧街凉风 2024-08-29 18:06:25

您可以通过比较两个日期来对日期进行排序。为此,您应该使用。 NSComparisonResult 类。
NSComparisonResult 结果=[日期1比较:日期2];

如果结果==NSorderSame
那么 date1 和 date2 是相同的。

结果==NSOrderedAscending
那么 date1 小于 date2。

结果==NSOrderedDescending
那么 date1 大于 date2。

You can do sorting of date by comparing two dates.For that you should use. NSComparisonResult class.
NSComparisonResult result=[date1 compare:date2];

if result==NSorderSame
then date1 and date2 is Same.

result==NSOrderedAscending
then date1 is smaller than date2.

result==NSOrderedDescending
then date1 is greater than date2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文