Skip to main content

$$sort

Sorts elements of an array

Usage​

{
"$$sort": [ /* values */ ],
"by": /* Transformer */,
"order": /* direction of ordering */,
"type": /* type of values (to order by) */,
"then": [
{
"by": /* Transformer */,
"order": /* direction of ordering */,
"type": /* type of values (to order by) */,
},
...
]
}

Returns​

array (same as input)

Arguments​

ArgumentTypeValuesRequired / Default ValueDescription
PrimaryarrayYesArray of elements
byTransformerInput is ##current (current element)##currentA transformer to extract a property to sort by (should resolve to one of the supported types)
orderEnumASC/DESCASCDirection of ordering (ascending / descending)
typeEnumauto/string/number/booleanautoType of values to expect when ordering the input array
thenarrayArray of items containing by,order & typeAn array of secondary sorting in case previous sorting returned equal, first being the root fields. (Every item require the by field while other fields are optional)

Examples​

Input

Definition

Output

["b","c","a"]
{ 
"$$sort":"$"
}
["a","b","c"]
[2,3,1]
{ 
"$$sort":"$"
}
[1,2,3]
[2,3,1]
{ 
"$$sort":"$",
"order":"DESC"
}
[3,2,1]
[
[0,"b"],
[1,"c"],
[2,"a"]
]
{ 
"$$sort":"$",
"by":"##current[1]"
}
[
[2,"a"],
[0,"b"],
[1,"c"]
]
[
"D1",
"D4",
"C2",
"B1",
"B2",
"B3",
"A2"
]
{
"$$sort": "$",
"by": "$$substring(0,1):##current",
"order":"DESC",
"then": [
{ "by": "$$long:$$substring(-1):##current", "order": "DESC" }
]
}
[
"D4",
"D1",
"C2",
"B3",
"B2",
"B1",
"A2"
]