内置函数
orderBy()
也可以对对象进行排序。但是,要对子对象进行排序,您需要一个应用的递归函数
orderBy()
给按类型匹配的孩子。
%dw 2.0
output application/json
fun sortObjects(x)=
x match {
case o is Object -> o
orderBy ((value, key) -> key)
mapObject (($$): sortObjects($))
else -> $
}
---
sortObjects(payload)
输出
{
"Footer": {
"accountName2": "Account Receivable (Debtors)",
"itemQuantity2": "49.5",
"productDescription2": "Maggie",
"taxName2": "TAX"
},
"Header": {
"accountName3": "Account Receivable (Debtors)",
"itemDiscount3": "10",
"productPrice3": "10",
"taxName3": "TAX"
},
"Memo": {
"accountName1": "Account Receivable (Debtors)",
"itemAmount1": "5",
"productPrice1": "10",
"taxName1": "TAX"
}
}
请注意,将匹配项添加到
Array
您可以使用该函数同时对数组进行排序。