4

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.

1
  • very good question i follow your post for the answer...nice attempt :) Commented Sep 2, 2013 at 6:19

2 Answers 2

4

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;
        }];
Sign up to request clarification or add additional context in comments.

4 Comments

This will just sort it but it will not return the unique combination of (month and year)
Sorry, but this answer is wrong. 1) You cannot compare NSDate objects with <. 2) A comparator must return NSOrderedAscending (-1), NSOrderedSame (0), or NSOrderedDescending (+1), not YES or NO. - The question explicitly states "... timestamp. (This is of NSDate type.)".
@ShafKhan: Even if timestamp is scalar property so that you can compare with <: Returning only YES or NO from a comparator is wrong. Have a look at the documentation of NSComparisonResult.
@ShafKhan: "As NSDate timestamp returns NSTimeInterval"?? - NSDate does not have a timestamp method.
2

This should work:

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

1 Comment

This will just sort it but it will not return the unique combination of (month and year).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.