Note that there are some explanatory texts on larger screens.

plurals
  1. POSort NSDictionaries in NSMutableArray by NSDate
    primarykey
    data
    text
    <p>I have a NSMutableArray with dictionaries, all of the dict's contain a NSDate is form of NSString with key 'Date'. I want to sort the NSDictionaries by the Dates. So for example i have the following state of the array:</p> <pre><code>Dict Date 20.06.1996 23:30 Dict Date 04.10.2011 19:00 Dict Date 20.06.1956 23:39 </code></pre> <p>And I want to sort it, so that it looks like this:</p> <pre><code>Dict Date 20.06.1956 23:39 Dict Date 20.06.1996 23:30 Dict Date 04.10.2011 19:00 </code></pre> <p>I have already experimented with NSSortDescriptor, but without success...</p> <p>Update:</p> <p>I have managed to sort the dates, but I came to this problem: In the dicts there is not only dates, also other objects, and what my code does is it only switches the date values between the dicts, instead of switching the complete dicts around. With this, the other values in the dicts get assigned a wrong date, which is very bad. Can anybody help me? Heres my code:</p> <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedData.daf"]; NSMutableArray *d = [NSMutableArray arrayWithContentsOfFile:path]; for (int ii = 0; ii &lt; [d count]; ii++) { NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; if (is24h) { [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; } else { [dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"]; } [dateFormatter setLocale:[NSLocale currentLocale]]; NSDate *dat = [dateFormatter dateFromString:[[d valueForKey:@"Date"] objectAtIndex:ii]]; NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii]; [newDict addEntriesFromDictionary:oldDict]; [newDict setObject:dat forKey:@"Date"]; [d replaceObjectAtIndex:ii withObject:newDict]; [newDict release]; } NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:YES]; NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil]; [sorter release]; NSMutableArray *sorted = [NSMutableArray arrayWithArray:[d sortedArrayUsingDescriptors:sorters]]; [sorters release]; NSLog(@"%@",sorted); for (int ii = 0; ii &lt; [sorted count]; ii++) { NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; if (is24h) { [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; } else { [dateFormatter setDateFormat:@"dd.MM.yyyy hh:mm a"]; } [dateFormatter setLocale:[NSLocale currentLocale]]; NSString *sr = [dateFormatter stringFromDate:[[sorted valueForKey:@"Date"] objectAtIndex:ii]]; NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init]; NSDictionary *oldDict = (NSDictionary *)[d objectAtIndex:ii]; [newDict addEntriesFromDictionary:oldDict]; [newDict setObject:sr forKey:@"Date"]; [sorted replaceObjectAtIndex:ii withObject:newDict]; [newDict release]; } NSLog(@"before: %@" "" "after: %@",d,sorted); [sorted writeToFile:path atomically:YES]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload