排序NSDictionary [英] Sort NSDictionary
问题描述
我得到了 NSDictionary ,它们与:
Key -> Value:
@"home" -> @"blue"
@"family" -> @"green"
@"work" -> @"red"
已更新
互联网说没有人可以对NSDictionary进行排序:c
Internet said no one can sort NSDictionary :c
这段代码帮助我实现了价值观.
This code helped me with values.
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
但是我失去了键和值之间的关系.如果有人可以编写函数,该函数采用NSDictionary并生成两个NSArray,它们相互关联,并且其中一个数组按NSDictionary的值排序,如下所示:
But i lose relation between keys and values. If anybody can write function, which take NSDictionary and make two NSArray, which related and one of those arrays sorted by values of NSDictionary like this:
(unsorted)
Key -> Value:
@"work" -> @"red"
@"home" -> @"blue"
@"family" -> @"green"
转换为:
Arrays, which keep relationship by index of item and one of them (values of NSDictionary) sorted
ArrayOfKeys = {@"home",@"family",@"work"}
ArrayOfValues = {@"blue", @"green", @"red"} -- this one sorted like alphabet
推荐答案
您可以使用keysSortedByValueUsingComparator
,keysSortedByValueWithOptions
或keysSortedByValueUsingSelector
之类的方法以排序顺序访问键.但是根据定义,NSDictionary
本身没有概念或顺序.
You can access the keys in a sorted order with method like keysSortedByValueUsingComparator
, keysSortedByValueWithOptions
or keysSortedByValueUsingSelector
. But by definition a NSDictionary
itself has no notion or order.
这篇关于排序NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!