对 Date[dd/MM/yyyy] 进行排序,该数据类型是 Objective-C for iPhone 中数组内的 NSString 数据类型
我有一个 NSMutableArray,它保存 NSString 类型的 dd/MM/yyyy 格式的日期。 现在我需要显示排序后的日期数组。请建议我如何实现它。
谢谢。
I have an NSMutableArray which holds the dates in format dd/MM/yyyy of type NSString.
Now i need to display the sorted array of dates. Please suggest me how to achieve it.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
我可以想到两个选择:
您可以使用 NSMutableArray 的
sortUsingFunction
或sortUsingSelector
等方法。创建接受字符串参数(数组对象的数据类型)的排序方法或函数,使用 NSDateFormatter 的dateFromString
方法将字符串转换为日期,使用 NSDate 的compare
方法比较两个日期并返回比较结果。有关数组排序函数的典型示例,请参阅 NSArray 类引用 -sortedArrayUsingFunction。自己实现任何典型的排序算法(例如快速排序)。在实现中,在比较两个元素之前将日期转换为字符串。
I can think of two options:
You can use NSMutableArray's
sortUsingFunction
orsortUsingSelector
etc. methods. Create the sort method or function which takes string parameters (array object's datatype), converts the strings to dates using NSDateFormatter'sdateFromString
method, compares the two dates using NSDate'scompare
method and return the comparison result. For a typical example of the sort function for an array, see NSArray class reference - sortedArrayUsingFunction.Implement any of typical sort algorithms (like quick sort) yourself. In the implementation convert the date to string before you compare two elements.