将NSDictionary键排序为NSDate [英] Sort NSDictionary keys as NSDate

查看:66
本文介绍了将NSDictionary键排序为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的SDK和XCode 4.2开发iOS 4应用程序.

I'm developing an iOS 4 application with latest SDK and XCode 4.2.

我有一个NSMutableDictionary,其中NSDateNSMutableString.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆