5

I have an array of dictionaries. Within each dictionary, there is a a key dateOfInfo (an NSDate) and several other things. I want to sort the array by each dictionaries dateOfInfo with the most recent being the first result.

How can I do this?

PengOne
  • 48,188
  • 17
  • 130
  • 149
Baub
  • 5,004
  • 14
  • 56
  • 99

3 Answers3

17

You can sort using NSSortDescription, e.g.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey: @"dateOfInfo" ascending: NO];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];

You can also use the method

- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context
PengOne
  • 48,188
  • 17
  • 130
  • 149
  • This is just about perfect. I used the first part of your code but had to change the Ascending to NO and instead of sortUsingDescriptors I had to use sortedArrayUsingDescriptors. Other than that, this worked wonderfully. Thanks yet again, PengOne! – Baub Sep 21 '11 at 23:10
  • @James: Glad it worked. I updated my answer to incorporate your revisions. – PengOne Sep 21 '11 at 23:15
  • I have array of dictionaries and I am sorting by age and bank number, but its not working at all. – Satish A Mar 11 '16 at 13:49
1

The basic bubble sorting algorithm would work. In this case you need to loop through your array, use valueForKey message on [array objectAtIndex:] to get the NSDate values. For comparing dates see this post . So if you are sorting in ascending order of date, just add the object with the lower date (remember bubble sort comparisons?) to an array which will hold your sorted result.

Community
  • 1
  • 1
Bourne
  • 10,094
  • 5
  • 24
  • 51
  • Thanks for the link to that post. Working on it now. – Baub Sep 21 '11 at 22:43
  • The second answer is easier too using a NSSortDescriptor. But the way my brain went on this shows one thing. I'm still an algorithms and C guy. Throwing fancy classes at me still doesn't work :) – Bourne Sep 21 '11 at 22:48
  • 2
    `NSArray` has a lot of powerful built-in sorting functionality; there's no need to do this by hand. – jlehr Sep 21 '11 at 22:49
  • 3
    And to the person who downvoted this: A downvote is for an answer that isn't useful. Hope you knew the meaning of that sentence. The answer isn't wrong or useless. Just that there exists an easier way using NSSortDescriptor – Bourne Sep 21 '11 at 22:56
  • I was able to get it to work this way, but I replaced it with PengOne's code because it was quite a bit shorter and much faster. Thank you though! :) – Baub Sep 21 '11 at 23:11
0

try like this,

            NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
             [fmtDate setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
             NSComparator compareDates = ^(id string1, id string2)
             {
                 NSDate *date1 = [fmtDate dateFromString:string1];
                 NSDate *date2 = [fmtDate dateFromString:string2];

                 return [date1 compare:date2];
             };
             NSSortDescriptor * sortDesc1 = [[NSSortDescriptor alloc] initWithKey:@"StartTime" ascending:YES comparator:compareDates];
             [youArray sortUsingDescriptors:@[sortDesc1]];
Balu
  • 8,470
  • 2
  • 24
  • 41