[英]Sort array of Custom Objects by date range Swift
I am having an array of Custom Objects and each object having a date property of NSDate.I want to sort now the Array by using a start and end date.I want to get all the objects whose date matches with start date and end date and in between dates.我有一个自定义对象数组,每个对象都有一个 NSDate 的日期属性。我现在想通过使用开始日期和结束日期对数组进行排序。我想获取日期与开始日期和结束日期匹配的所有对象以及在日期之间。
i really do not know about doing this.Any suggestions and sample code are much helpful and appreciated.我真的不知道这样做。任何建议和示例代码都非常有帮助和赞赏。
The simpliest way you can do this is:最简单的方法是:
- (NSArray *)selectObjectsWithBeginDate:(NSDate *)dateBegin endDate:(NSDate *)dateEnd {
NSMutableArray *selections = [NSMutableArray array];
for (CustomObject *obj in objectArray) {
if ([obj.date compare:dateBegin] == NSOrderedDescending && [obj.date compare:dateEnd] == NSOrderedAscending) {
[selections addObject:obj];
}
}
return [NSArray arrayWithArray:selections];
}
You just need to be able ti compare two NSDate objects.您只需要能够比较两个 NSDate 对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.