我可以根据 Objective-C 中的键对 NSDictionary 进行排序吗? [英] Can i sort the NSDictionary on basis of key in Objective-C?

查看:22
本文介绍了我可以根据 Objective-C 中的键对 NSDictionary 进行排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以根据关键字对 NSDictionary 进行排序吗?

Can I sort the NSDictionary on basis of key?

推荐答案

您可以对键进行排序,然后通过迭代它们来创建一个 NSMutableArray.

You can sort the keys and then create an NSMutableArray by iterating over them.

NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys)
    [sortedValues addObject: [dict objectForKey: key]];

这篇关于我可以根据 Objective-C 中的键对 NSDictionary 进行排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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