Swift:按键对[String:String]()进行排序

时间:2018-06-30 13:00:25

标签: arrays swift sorting

我使用以下代码:

var names = [String: String]()
names["1"] = "test"
names["3"] = "new"
names["2"] = "myname"
names["4"] = "yourname"

我打印并得到以下结果:

["1": "test", "3": "new", "2": "myname", "4": "yourname"]

我要按names排序key并使用以下代码:

names.sorted(by: { $0.key.first! < $1.key.first! }

但是我的结果是:

[(key: "1", value: "test"), (key: "2", value: "myname"), (key: "3", value: "new"), (key: "4", value: "yourname")]

我要删除key:value:并显示如下:

["1": "test", "2": "myname", "3": "new", "4": "yourname"]

0 个答案:

没有答案