LoginSignup
0
0

More than 5 years have passed since last update.

Swiftの辞書(Dictionary)をkeyの長さでsortする。

Posted at
var dic:[String:String] = ["しし":"cc", "お":"o", "あああ":"aaa"]
print(dic.sorted { return $0.key.characters.count < $1.key.characters.count })
// -> [(key: "お", value: "o"), (key: "しし", value: "cc"), (key: "あああ", value: "aaa")]

戻ってくるのは「sortされた辞書」ではなく,[(key,value)] (※key,valueを内容とするタプルの配列)のようだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0