简体   繁体   English

根据NSArray元素对NSDictionary键进行排序

[英]Sort NSDictionary keys according to NSArray elements

I have NSDictionary filled with many objects. 我的NSDictionary充满了许多对象。 I have sorted the keys in a desired pattern and now I have the keys only in a new NSArray . 我已按所需模式对键进行了排序,现在仅在新的NSArray具有键。 How to apply the changes so the order of the elements in the array to be reflected in the dictionary? 如何应用更改,使数组中元素的顺序反映在字典中?
For ex. 对于前。 NSArray -> ["D" , "B" , "A" , "C"] this order I want to apply to the keys in the dictionary. NSArray -> ["D" , "B" , "A" , "C"]我要将此顺序应用于字典中的键。

NSDictionary -> {{"A" : <something>} , {"B" : <something>} , {"C" : <something>} , {"D" : <something>}}

You don't. 你不知道 You iterate over the keys in the array and then pull out the data from the dictionary. 您遍历数组中的键,然后从字典中拉出数据。

for (NSString *key in sortedKeys) {
    id value = dictionary[key];
    // do something...
}

If you need ordering you might consider using NSOrderedSet or rolling your own data structure to do this. 如果需要订购,则可以考虑使用NSOrderedSet或滚动自己的数据结构来执行此操作。 For example, I have created data structures that use both NSIndexSet and NSDictionary to provide key/value storage with faster sorted enumeration. 例如,我创建了同时使用NSIndexSet和NSDictionary的数据结构,以提供具有更快排序枚举的键/值存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM