将NSDictionary键排序为NSDate [英] Sort NSDictionary keys as NSDate
问题描述
我正在使用最新的SDK和XCode 4.2开发iOS 4应用程序.
I'm developing an iOS 4 application with latest SDK and XCode 4.2.
我有一个NSMutableDictionary
,其中键是NSDate
,值是NSMutableString
.
I have a NSMutableDictionary
where keys are NSDate
and values are NSMutableString
.
填满 NSMutableDictionary 后,我想对它的键进行排序,但是我不知道该怎么做.
When I have filled up the NSMutableDictionary I want to sort its keys but I don't know how can I do it.
我想要第一个1月日期,然后是2月,依此类推.
I want first January dates, then February and so on.
NSDate
密钥格式为 dd/mm/yyyy .
当NSMutableDictionary
具有所有键和值时,如何排序这些键? (我不会添加更多内容.)
How can I sort those keys when NSMutableDictionary
has all its keys and values? (I'm not going to add more).
推荐答案
这是很标准的事情,但是,您可能不 sort NSDictionary-您可以对从字典中获得的数组进行排序
That's a pretty standard thing, however, you may not sort an NSDictionary - you may sort an array you get from the dictionary however.
这就是我要做的:
// get all keys into array
NSArray * keys = [your_dictionary allKeys];
// sort it
NSArray * sorted_keys = [keys sortedArrayUsingSelector:@selector(compare:)];
// now, access the values in order
for (NSDate * key in sorted_keys)
{
// get value
NSMutableString * your_value = [your_dictionary valueForKey: key];
// perform operations
}
这篇关于将NSDictionary键排序为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!