0

I have an NSArray with 3 objects in it. Each object is made up of 5 values. How can I sort by Date with in the objects?

result: (
    gg,
    "2012-10-28 01:34:00 +0000",
    "Church Bells",
    "pin_red",
    1
)(
    iu,
    "2008-09-22 17:32:00 +0000",
    "Birthday Song",
    "pin_red",
    1
)(
    "my birthday woo hoo",
    "2012-09-04 19:27:00 +0000",
    "Birthday Song",
    "pin_blue",
    1
)

The results I am looking for - Sorted Array should look like this.

(
    iu,
    "2008-09-22 17:32:00 +0000",
    "Birthday Song",
    "pin_red",
    1
)
(
    "my birthday woo hoo",
    "2012-09-04 19:27:00 +0000",
    "Birthday Song",
    "pin_blue",
    1
)
(
    gg,
    "2012-10-28 01:34:00 +0000",
    "Church Bells",
    "pin_red",
    1
)

I am getting this array from my nsdictionary object.

dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath];
stringsMutableArray = [[NSMutableArray alloc] initWithObjects:nil];

        for (id key in dictionary) 
        {
            [stringsMutableArray addObject:[dictionary objectForKey:key]];
        }
3
  • This answer: stackoverflow.com/a/805589/937822 shows you exactly how to do this. Commented Oct 28, 2012 at 4:43
  • @inafziger. I have looked at that solution over and over again and I don't see it. As you can see I don't have key's associated with my date. That post didn't help me. Commented Oct 28, 2012 at 4:47
  • What do you mean that you don't have keys? If this is an NSArray, then it must contain objects. Those objects must have names for each of their properties.... If you are unable to adapt that code, then post the code that you have for creating the array and we can be more specific. Commented Oct 28, 2012 at 4:49

2 Answers 2

2

Try this:

NSArray *sortedArray = [result sortedArrayUsingComparator: ^(id obj1, id obj2) {
    NSArray *arr1 = (NSArray *)obj1;
    NSArray *arr2 = (NSArray *)obj2;
    NSDate *date1 = arr1[1];
    NSDate *date2 = arr2[1];

    return [date1 compare:date2];
}];

This code assumes you actually have an array of arrays and the dates are NSDate objects always at index 1 in the inner arrays. If this sorts in the opposite order you want, swap the two dates for the date comparison.

Sign up to request clarification or add additional context in comments.

2 Comments

prefect! it works that's exactly what I was looking for. Though I had to do it this way NSDate *date1 = [arr1 objectAtIndex:1]; NSDate *date2 = [arr2 objectAtIndex:1];
The syntax I used is for use with modern Objective-C. If that didn't work then you must be using an older version.
0

Here's a similar question that solves your issue: Sort NSArray of date strings or objects

I'll paste the answer from the above link below:

Store the dates as NSDate objects in an NS(Mutable)Array, then use [-[NSArray sortedArrayUsingSelector:][1] or [-[NSMutableArray sortUsingSelector:]][1] and pass @selector(compare:) as the parameter. The [-[NSDate compare:]][2] method will order dates in ascending order for you. This is simpler than creating an NSSortDescriptor, and much simpler than writing your own comparison function. (NSDate objects know how to compare themselves to each other at least as efficiently as we could hope to accomplish with custom code.)

[1]: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/sortUsingSelector: [2]: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDate/compare:

2 Comments

this solution doesn't fit me. They want me to put all the dates into an array, sort that array. What is that going to get me? I mean I still need to get my original array sorted.
the @selector can be used for a custom compare method in which you can access the individual members and use them to sort

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.