按对象的NSDate属性对数组进行排序 [英] Sort array of objects by their NSDate property

查看:122
本文介绍了按对象的NSDate属性对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何对其中包含自定义对象的NSMutableArray进行排序? /a>

Possible Duplicate:
How to sort an NSMutableArray with custom objects in it?

如何通过访问对象的NSDate属性对数组进行排序?

How does one sort an array of objects by accessing their NSDate properties?

我知道一个人可以在日期数组上使用[mutableArray sortUsingSelector:@selector(compare:)];,但是如何通过访问数组中每个元素的NSDate属性并按最早日期到最新日期对对象进行排序呢?

I know one can use [mutableArray sortUsingSelector:@selector(compare:)]; on an array of dates, but how does one do it by accessing a NSDate property on each element in the array, sorting the objects by earliest date to latest date?

推荐答案

感谢所有答案,我遇到了解决了我问题的答案.

Thanks for all the answers, I came across this answer that solved my problem.Check NSGod's answer

以下是感谢用户NSGod的代码:

Here is the code thanks to user: NSGod:

NSSortDescriptor *dateDescriptor = [NSSortDescriptor
                                 sortDescriptorWithKey:@"startDateTime" 
                                             ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray
     sortedArrayUsingDescriptors:sortDescriptors];

这篇关于按对象的NSDate属性对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆