简体   繁体   中英

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.)

Now I need to get the objects A filtered in different arrays for (MOnth and Year as unique key).

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]]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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