3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Arrayの中のDictionaryの中のValueでのSort

Last updated at Posted at 2016-01-21

Swift2.1 での Arrayの中にDictionary([[String:String]])の中のValueでのSort

Swift2.1 で Sort系がかなり便利になっていたのを今更ながら実感したので備忘録的に。(自分用が主w)

let family : [[String: String]] = [
["surname":"Simpson", "given":"Homer", "title": "Mr"],
["surname":"Simpson", "given":"Marge", "title": "Mrs"],
["surname":"Simpson", "given":"Bart", "title": "Mr"],
["surname":"Simpson", "given":"Lisa", "title": "Miss"],
["surname":"Simpson", "given":"Maggie", "title": "Miss"],
["surname":"Flanders", "given":"Ned", "title": "Mr"]
 ]

let orderdfamiry = family.sort() {
    $0["given"] < $1["given"]
}

print(orderdfamily)
> [["surname": "Simpson", "title": "Mr", "given": "Bart"],
 ["surname": "Simpson", "title": "Mr", "given": "Homer"],
 ["surname": "Simpson", "title": "Miss", "given": "Lisa"],
 ["surname": "Simpson", "title": "Miss", "given": "Maggie"],
 ["surname": "Simpson", "title": "Mrs", "given": "Marge"],
 ["surname": "Flanders", "title": "Mr", "given": "Ned"]]

こんな感じで。
sort周り混乱というかいろんな書き方あったけど、ようやくまとまってきたのかな?

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?