我想基于两个键(compartmentNumber& row)在JSON下面排序,并尝试下面提到的代码示例,通过一个错误。
{
"response": {
"flightLegId": "FL004",
"loadable": [{
"position": {
"deckNumber": 1,
"compartmentNumber": 1,
"leftRight": "ALL",
"row": 1
},
"uld": {
"uldAirline": "KL",
"uldNumber": "ULD007",
"uldPrefix": "PAG",
"commodity": [{
"commodityCode": "C",
"airportCode": "GLA",
"hasDangerousGoods": false,
"pieceCount": 0,
"weightKg": 710
}]
}
}, {
"position": {
"deckNumber": 1,
"compartmentNumber": 1,
"leftRight": "ALL",
"row": 2
},
"uld": {
"uldAirline": "KL",
"uldNumber": "ULD007",
"uldPrefix": "PAP",
"commodity": [{
"commodityCode": "C",
"airportCode": "GLA",
"hasDangerousGoods": false,
"pieceCount": 5,
"weightKg": 780
}, {
"commodityCode": "M",
"airportCode": "GLA",
"hasDangerousGoods": false,
"pieceCount": 5,
"weightKg": 780
}]
}
}]
}
}
func sortResults(items: [Loadable]) -> [Loadable] {
var sortResults: [Loadable] = []
if let sortedArray = (items as NSArray).sortedArray(using: [
NSSortDescriptor(key: "compartmentNumber", ascending: true),
NSSortDescriptor(key: "row", ascending: true),
]) as? [Loadable] {
sortResults = sortedArray
}
return sortResults
}
答案 0 :(得分:0)
使用此功能
func sortResults(items: [Loadable]) -> [Loadable] {
var sortResults: [Loadable] = []
if let sortedArray = (items as NSArray).sortedArray(using: [
NSSortDescriptor(key: "position.compartmentNumber", ascending: true),
NSSortDescriptor(key: "position.row", ascending: true),
]) as? [Loadable] {
sortResults = sortedArray
}
return sortResults
}