如何用键值对NSMutableDictionary进行排序? [英] how can I sort NSMutableDictionary with keys value?

查看:292
本文介绍了如何用键值对NSMutableDictionary进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSMutableDictionary ,带有字符串键,每个键都有自己的数组。我想按字母顺序用键值重新排序字典。我怎么能这样做?

I have a NSMutableDictionary with string keys and every key has its own array. I want to re-sort the dictionary with keys value alphabetically. How can I do this?

推荐答案

根据定义,字典是未排序的。

A dictionary is unsorted by definition.

为了按特定顺序迭代键,您可以对包含字典键的数组进行排序。

For iterating over the keys in a specific order, you can sort an array containing the keys of the dictionary.

NSArray *keys = [theDictionary allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compareMethod:)];

还有其他几个已排序...... 方法,例如使用 NSComparator 进行排序。看看此处

There are several other sorted... methods, e.g. sorting with a NSComparator. Have a look here.

这篇关于如何用键值对NSMutableDictionary进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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