对 Date[dd/MM/yyyy] 进行排序,该数据类型是 Objective-C for iPhone 中数组内的 NSString 数据类型

发布于 2024-09-16 01:55:54 字数 100 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

明天过后 2024-09-23 01:55:54

我可以想到两个选择:

  1. 您可以使用 NSMutableArray 的 sortUsingFunctionsortUsingSelector 等方法。创建接受字符串参数(数组对象的数据类型)的排序方法或函数,使用 NSDateFormatter 的 dateFromString 方法将字符串转换为日期,使用 NSDate 的 compare 方法比较两个日期并返回比较结果。有关数组排序函数的典型示例,请参阅 NSArray 类引用 -sortedArrayUsingFunction

  2. 自己实现任何典型的排序算法(例如快速排序)。在实现中,在比较两个元素之前将日期转换为字符串。

I can think of two options:

  1. You can use NSMutableArray's sortUsingFunction or sortUsingSelector etc. methods. Create the sort method or function which takes string parameters (array object's datatype), converts the strings to dates using NSDateFormatter's dateFromString method, compares the two dates using NSDate's compare method and return the comparison result. For a typical example of the sort function for an array, see NSArray class reference - sortedArrayUsingFunction.

  2. Implement any of typical sort algorithms (like quick sort) yourself. In the implementation convert the date to string before you compare two elements.

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