iOS
Swift
Swift2.1

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

More than 1 year has passed since last update.

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周り混乱というかいろんな書き方あったけど、ようやくまとまってきたのかな?