简体   繁体   English

如何根据NSArray中自定义对象的日期进行排序...?

[英]How to sort based on date of custom object in NSArray…?

I have an Array of custom objects. 我有一个自定义对象数组。 Say the object is called A and this has a property called timestamp.(This is of NSDate type.) 假设该对象被称为A,并且它具有一个名为timestamp的属性。(这是NSDate类型。)

Now I need to get the objects A filtered in different arrays for (MOnth and Year as unique key). 现在我需要将对象A在不同的数组中过滤(MOnth和Year作为唯一键)。

I have been trying to use predicates but unable to figure out how to put it on date objects. 我一直在尝试使用谓词,但无法弄清楚如何将它放在日期对象上。

You can look at the image below to see where exactly I need to use it... 您可以查看下面的图片,看看我需要在哪里使用它......

EDIT : Removing image. 编辑 :删除图像。

Here is the Code snippet which can help you 以下是可以帮助您的代码段

NSArray *sortedArray = [unSortedArray  sortedArrayUsingComparator: ^NSComparisonResult(DateObj *id1, DateObj *id2) {
            if(id1.timeStamp < id2.timeStamp)
                return YES;
            else
                return NO;
        }];

This should work: 这应该工作:

[myArray sortUsingDescriptors:[NSArray arrayWithObject:
                                  [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO]]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM