iPhone: How to Sort Dates Using NSDate?

你离开我真会死。 提交于 2019-12-12 01:45:56

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2334150/iphone-how-to-sort-dates-using-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!