对 NSDictionary 进行排序(其中包含我的 Facebook 好友列表)
通过此代码使用 Graph Facebook API
[facebook requestWithGraphPath:@"me/friends" andDelegate:self];
现在我有一个 NSDictionary,如下所示:
{data = (
{
id = 123456;
name = "Mark Zuckerberg";
},
{
id = 654321;
name = "Steve Jobs";
},
{
id = 13579;
name = "Bill Gates";
}
...
);
}
我想对此 NSDictionary 进行排序,但我知道这是不可能的。 当然,我可以创建2个NSArray(或NSMutableArray),一个“array_id”,另一个“array_name”。 我想对“array_name”进行排序,但保留对 id 的引用。
例如,如果我的两个数组是这样的:
array_name = {"Mark", "Steve", "Bill};
array_id = {"123456", "654321", "13579"};
最后我想要这样的东西:
array_name = {"Bill", "Mark", "Steve"};
array_id = {"13579", "123456", "654321"};
你对我有什么建议?谢谢!
Using the Graph Facebook API with this code
[facebook requestWithGraphPath:@"me/friends" andDelegate:self];
Now I have an NSDictionary which is like this:
{data = (
{
id = 123456;
name = "Mark Zuckerberg";
},
{
id = 654321;
name = "Steve Jobs";
},
{
id = 13579;
name = "Bill Gates";
}
...
);
}
I want to sort this NSDictionary, but I know that is not possible.
Of course, I can create 2 NSArray (or NSMutableArray), one "array_id" and the other one "array_name".
I'd like to sort "array_name", but keeping the reference to the id.
For example, if my two arrays are something like this:
array_name = {"Mark", "Steve", "Bill};
array_id = {"123456", "654321", "13579"};
I want, at the end, something like this:
array_name = {"Bill", "Mark", "Steve"};
array_id = {"13579", "123456", "654321"};
What do you suggest me? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您不想对
NSDictionary
进行排序,而是想对NSDictionary
对象的NSArray
进行排序,而该对象本身恰好是在NSDictionary
中。您可以简单地提取这个朋友数组,如下所示:现在您可以执行以下操作:
It doesn't look like you want to sort an
NSDictionary
, you want to sort anNSArray
ofNSDictionary
objects that just so happens to itself be in anNSDictionary
. You can simply extract this friends array as follows:Now you can do the following:
您可以使用 NSArray 的
sortedArrayUsingComparator:
方法来完成此操作。比较器块将返回比较其参数名称的结果。You can do this using the
sortedArrayUsingComparator:
method of NSArray. The comparator block would return the result of comparing the names of its arguments.简单的事情就是创建一种新的对象:
当你有了这个对象时,你可以循环遍历你返回的数组并执行以下操作:
完成后,对事物进行排序就很简单:
The simple thing would be to create a new kind of object:
When you have this, you can loop through your array that you got back and do:
Once you've done that, it's simple to sort things:
您可以使用 3 种方法中的任何一种
-keysSortedByValueUsingSelector:
,-keysSortedByValueUsingComparator:
,或
-keysSortedByValueWithOptions:usingComparator:
获取按顺序排序的键数组。因此,如果您想按顺序迭代,可以执行以下操作:如果您需要一个单独的数组,其中包含与键的排序顺序相对应的顺序的所有值,则执行上述迭代并构造该数组。
You can use any of the 3 methods
-keysSortedByValueUsingSelector:
,-keysSortedByValueUsingComparator:
, or-keysSortedByValueWithOptions:usingComparator:
to get an array of the keys sorted in order. So, if you want to iterate in order, you can do something like this:If you need a separate array containing all of the values in the order corresponding to the sorted order of the keys, then do the above iteration and construct that array.