1

I use below code:

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

I print and get below result :

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

I want sort names by key and use this code :

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

But my result is :

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

I want remove key: and value: and show like below :

["1": "test", "2": "myname", "3": "new", "4": "yourname"]
salari mameri
  • 228
  • 5
  • 13
  • 7
    In Swift, `Dictionary` type does not preserve orders, so you cannot achieve what you want. If you can explain how you want to use the result, there may be many better alternatives than sorting the Dictionary itself. – OOPer Jun 30 '18 at 13:04

0 Answers0