Question: I have a `NSMutableDictionary`, that contains contact names (`NSString`'s) and the dates (`NSDate`'s). The names are the `keys`. **How do I sort the names by date created?**

I assume I have to create a `NSMutableArray` of the dates, then sort it somehow. **But then how do I get the names for the dates? How do I even sort `NSDate`'s?** 

Thanks!

Answer: 
Try the following code if the dict contains NSString in date format:

```
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"your date format"];

NSArray *sortedKeys = [yourDictionary  keysSortedByValueUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2)
{
    NSDate *date1 = [dateFormatter dateFromString:obj1];
    NSDate *date2 = [dateFormatter dateFromString:obj2];
    return [date1 compare:date2];
}];

```

The sortedKeys will contain the sorted names.

If the dictionary contains actual NSDate objects then:

```
NSArray *sortedKeys = [yourDictionary  keysSortedByValueUsingComparator:^NSComparisonResult(NSDate *obj1, NSDate *obj2)
{
   return [obj1 compare:obj2];
}];

```
You can use `[myDictionary keysSortedByValueUsingSelector:@selector(compare:)];`