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]];
        }
Matthias Bauch
  • 88,097
  • 19
  • 217
  • 244
Sam B
  • 25,617
  • 14
  • 79
  • 115
  • This answer: http://stackoverflow.com/a/805589/937822 shows you exactly how to do this. – lnafziger Oct 28 '12 at 04: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. – Sam B Oct 28 '12 at 04: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. – lnafziger Oct 28 '12 at 04:49

2 Answers2

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.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • 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]; – Sam B Oct 28 '12 at 05:06
  • The syntax I used is for use with modern Objective-C. If that didn't work then you must be using an older version. – rmaddy Oct 28 '12 at 05:55
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:

Community
  • 1
  • 1
Cezary Wojcik
  • 21,065
  • 6
  • 34
  • 36
  • 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. – Sam B Oct 28 '12 at 04:37
  • the `@selector` can be used for a custom `compare` method in which you can access the individual members and use them to sort – Cezary Wojcik Oct 28 '12 at 05:04