如何从特定键对数组进行排序?
How to sort an array from a specific key?
目前,我有一个数组声明为:
let VitamineARetinol = "VitamineARetinol"
let VitamineABetacarotene = "VitamineABetacarotene"
let VitamineC = "VitamineC"
let VitamineD = "VitamineD"
let Calories = "Calories"
typealias ListOfAliment = [String: [String: Double]]
let Data: ListOfAliment = [
"Orange": [VitamineARetinol: 0.5, VitamineC: 0.3, VitamineD: 0.98, Calories: 100],
"Pomme": [VitamineARetinol: 0.2, VitamineC: 0.2, VitamineD: 0.38, Calories: 120],
"Poire": [VitamineARetinol: 0.1, VitamineC: 0.7, VitamineD: 0.58, Calories: 140],
"Laitue": [VitamineARetinol: 0.3, VitamineC: 0.1, VitamineD: 0.92, Calories: 105],
"Banane": [VitamineARetinol: 0.7, VitamineC: 0.01, VitamineD: 0.63, Calories: 122]
]
var ListOfAlimentClassed = Data
我需要从卡路里键对其进行排序,同时保留所有数组数据,如下所示:
ListOfAlimentClassed = [
"Orange": [VitamineARetinol: 0.5, VitamineC: 0.3, VitamineD: 0.98, Calories: 100],
"Laitue": [VitamineARetinol: 0.3, VitamineC: 0.1, VitamineD: 0.92, Calories: 105],
"Pomme": [VitamineARetinol: 0.2, VitamineC: 0.2, VitamineD: 0.38, Calories: 120],
"Banane": [VitamineARetinol: 0.7, VitamineC: 0.01, VitamineD: 0.63, Calories: 122],
"Poire": [VitamineARetinol: 0.1, VitamineC: 0.7, VitamineD: 0.58, Calories: 140]
]
我怎样才能在 Swift 中有效地做到这一点?请帮忙!
let sorted = ListOfAlimentClassed.sorted { (([=10=].1[Calories] as? Double) ?? 0) < ((.1[Calories] as? Double) ?? 0) }
为您的数据创建一个结构并改为使用数组
struct Aliment {
let name: String
let vitamineARetinol: Double
let vitaminC: Double
let vitaminD: Double
let calories: Double
}
var array = [
Aliment(name: "Orange", vitamineARetinol: 0.5, vitaminC: 0.3, vitaminD: 0.98, calories: 100),
Aliment(name: "Pomme", vitamineARetinol: 0.2, vitaminC: 0.2, vitaminD: 0.38, calories: 120),
]
array.sort(by: { [=10=].calories < .calories })
阅读 Swift Programming Language 书中关于结构的更多信息
目前,我有一个数组声明为:
let VitamineARetinol = "VitamineARetinol"
let VitamineABetacarotene = "VitamineABetacarotene"
let VitamineC = "VitamineC"
let VitamineD = "VitamineD"
let Calories = "Calories"
typealias ListOfAliment = [String: [String: Double]]
let Data: ListOfAliment = [
"Orange": [VitamineARetinol: 0.5, VitamineC: 0.3, VitamineD: 0.98, Calories: 100],
"Pomme": [VitamineARetinol: 0.2, VitamineC: 0.2, VitamineD: 0.38, Calories: 120],
"Poire": [VitamineARetinol: 0.1, VitamineC: 0.7, VitamineD: 0.58, Calories: 140],
"Laitue": [VitamineARetinol: 0.3, VitamineC: 0.1, VitamineD: 0.92, Calories: 105],
"Banane": [VitamineARetinol: 0.7, VitamineC: 0.01, VitamineD: 0.63, Calories: 122]
]
var ListOfAlimentClassed = Data
我需要从卡路里键对其进行排序,同时保留所有数组数据,如下所示:
ListOfAlimentClassed = [
"Orange": [VitamineARetinol: 0.5, VitamineC: 0.3, VitamineD: 0.98, Calories: 100],
"Laitue": [VitamineARetinol: 0.3, VitamineC: 0.1, VitamineD: 0.92, Calories: 105],
"Pomme": [VitamineARetinol: 0.2, VitamineC: 0.2, VitamineD: 0.38, Calories: 120],
"Banane": [VitamineARetinol: 0.7, VitamineC: 0.01, VitamineD: 0.63, Calories: 122],
"Poire": [VitamineARetinol: 0.1, VitamineC: 0.7, VitamineD: 0.58, Calories: 140]
]
我怎样才能在 Swift 中有效地做到这一点?请帮忙!
let sorted = ListOfAlimentClassed.sorted { (([=10=].1[Calories] as? Double) ?? 0) < ((.1[Calories] as? Double) ?? 0) }
为您的数据创建一个结构并改为使用数组
struct Aliment {
let name: String
let vitamineARetinol: Double
let vitaminC: Double
let vitaminD: Double
let calories: Double
}
var array = [
Aliment(name: "Orange", vitamineARetinol: 0.5, vitaminC: 0.3, vitaminD: 0.98, calories: 100),
Aliment(name: "Pomme", vitamineARetinol: 0.2, vitaminC: 0.2, vitaminD: 0.38, calories: 120),
]
array.sort(by: { [=10=].calories < .calories })
阅读 Swift Programming Language 书中关于结构的更多信息