Ankit Srivastava
Ankit Srivastava

Reputation: 12405

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.

Upvotes: 4

Views: 872

Answers (2)

Andrey Gordeev
Andrey Gordeev

Reputation: 32499

This should work:

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

Upvotes: 2

Shaffiulla Khan
Shaffiulla Khan

Reputation: 10374

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

Upvotes: 4

Related Questions