Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中按字母顺序排列nsdictionary的所有键?_Ios_Swift_Sorting_Key_Nsdictionary - Fatal编程技术网

Ios 在swift中按字母顺序排列nsdictionary的所有键?

Ios 在swift中按字母顺序排列nsdictionary的所有键?,ios,swift,sorting,key,nsdictionary,Ios,Swift,Sorting,Key,Nsdictionary,我有一本以字母为键的字典。我想把那些钥匙按字母顺序分类。我尝试了许多方法,但在Sort()方法上出现了错误。有人能帮我吗 提前谢谢 注: 1) 我不想得到字典的排序数组 2) 我不想根据值对字典进行排序 (我得到了很多答案)您可以按以下方式对键进行排序: let dictionary: NSDictionary = ["a" : 1, "b" : 2] let sortedKeys = (dictionary.allKeys as! [String]).sorted(<) // ["a",

我有一本以字母为键的字典。我想把那些钥匙按字母顺序分类。我尝试了许多方法,但在Sort()方法上出现了错误。有人能帮我吗

提前谢谢

注: 1) 我不想得到字典的排序数组 2) 我不想根据值对字典进行排序
(我得到了很多答案)

您可以按以下方式对键进行排序:

let dictionary: NSDictionary = ["a" : 1, "b" : 2]
let sortedKeys = (dictionary.allKeys as! [String]).sorted(<) // ["a", "b"]
let dictionary:NSDictionary=[“a”:1,“b”:2]

让sortedKeys=(dictionary.allKeys as![String])在Swift 2.2中排序(

您可以这样按升序排序

let myDictionary: Dictionary = ["a" : 1, "b" : 2]
let sortedKeys = myDictionary.keys.sort()          // ["a", "b"]
let myDictionary: Dictionary = ["a" : 1, "b" : 2]
let sortedKeys = myDictionary.keys.sort(>)        // ["b", "a"]
降序

let myDictionary: Dictionary = ["a" : 1, "b" : 2]
let sortedKeys = myDictionary.keys.sort()          // ["a", "b"]
let myDictionary: Dictionary = ["a" : 1, "b" : 2]
let sortedKeys = myDictionary.keys.sort(>)        // ["b", "a"]

适用于Swift 3

    // Initialize the Dictionary
let dict = ["name": "John", "surname": "Doe"]

// Get array of keys

var keys = Array(dict.keys).sorted(by: >)
print(keys)

感谢您的快速回复:-)。其他的答案都是错误,让我痛苦万分!!!!!!无法获取错误“sort”:在ArrayTanks上调用“sort()”方法,这为我节省了很多麻烦。谢谢!它节省了我的时间。它似乎不喜欢“var键”行。你想用所有的钥匙吗?var keys=Array(dict.allKeys)。排序(按:>)编辑:仍然不喜欢这样。我已经转到:var keys=(dict.allKeys as![String])。排序(按:>)