Skip to main content

$$sort

Sorts elements of an array

Usage​

{ 
"$$sort": /* Array of elements */,
"by": "##current" /* Transformer(##current) */,
"order": "ASC" /* or DESC */,
"type": "AUTO" /* or STRING / NUMBER / BOOLEAN */,
"then": [ /* array */
{
"by": /* array */,
"order": "ASC" /* or DESC */,
"type": "AUTO" /* or STRING / NUMBER / BOOLEAN */,
},
...
]
}
"$$sort([by],[order],[type],[then]):{input}"
note

Concrete values in the usage example are default values.

Returns​

array - Same type as input array

Arguments​

ArgumentTypeValuesRequired / Default ValueDescription
PrimaryarrayYesArray of elements
byTransformer(##current)##currentA transformer to extract a property to sort by
  • ##current - Current element
orderenumASC/ DESCASCDirection of ordering (ascending / descending)
typeenumAUTO/ STRING/NUMBER/BOOLEANAUTOType of values to expect when ordering the input array
thenarraynullAn array of secondary sorting in case previous sorting returned equal, first being the root fields
then[].byarrayYesA transformer to extract a property to sort by
then[].orderenumASC/ DESCASCDirection of ordering (ascending / descending)
then[].typeenumAUTO/ STRING/NUMBER/BOOLEANAUTOType of values to expect when ordering the input array

Examples​

Input

Definition

Output

[
"c",
"a",
"b"
]
{
"$$sort": "$"
}
[
"a",
"b",
"c"
]
[
3,
1,
2
]
{
"$$sort": "$"
}
[
1,
2,
3
]
[
"c",
"a",
"b"
]
{
"$$sort": "$",
"order": "desc"
}
[
"c",
"b",
"a"
]
[
[
"c",
"a",
"b"
],
null,
"desc"
]
{
"$$sort": "$[0]",
"order": "$[2]"
}
[
"c",
"b",
"a"
]
[
4,
2,
1,
3
]
{
"$$sort": "$",
"type": "number"
}
[
1,
2,
3,
4
]
[
4,
2,
1,
3
]
{
"$$sort": "$",
"type": "number",
"order": "desc"
}
[
4,
3,
2,
1
]
[
false,
false,
true
]
{
"$$sort": "$",
"type": "boolean"
}
[
false,
false,
true
]
[
false,
false,
true
]
{
"$$sort": "$",
"type": "boolean",
"order": "desc"
}
[
true,
false,
false
]
[
{
"name": "Dan"
},
{
"name": "Alice"
},
{
"name": "Carl"
},
{
"name": "Bob"
}
]
{
"$$sort": "$",
"by": "##current.name"
}
[
{
"name": "Alice"
},
{
"name": "Bob"
},
{
"name": "Carl"
},
{
"name": "Dan"
}
]
[
{
"name": "Dan"
},
{
"name": "Alice"
},
{
"name": "Carl"
},
{
"name": "Bob"
}
]
{
"$$sort": "$",
"type": "string",
"order": "desc",
"by": "##current.name"
}
[
{
"name": "Dan"
},
{
"name": "Carl"
},
{
"name": "Bob"
},
{
"name": "Alice"
}
]
[
"c",
"a",
"b"
]
"$$sort:$"
[
"a",
"b",
"c"
]
[
3,
1,
2
]
"$$sort:$"
[
1,
2,
3
]
[
"c",
"a",
"b"
]
"$$sort(##current,DESC):$"
[
"c",
"b",
"a"
]
[
[
"c",
"a",
"b"
],
null,
"desc"
]
"$$sort(##current,$[2]):$[0]"
[
"c",
"b",
"a"
]
[
4,
2,
1,
3
]
"$$sort(##current,ASC,NUMBER):$"
[
1,
2,
3,
4
]
[
4,
2,
1,
3
]
"$$sort(##current,DESC,NUMBER):$"
[
4,
3,
2,
1
]
[
false,
false,
true
]
"$$sort(##current,ASC,BOOLEAN):$"
[
false,
false,
true
]
[
false,
false,
true
]
"$$sort(##current,DESC,BOOLEAN):$"
[
true,
false,
false
]
[
{
"name": "Dan"
},
{
"name": "Alice"
},
{
"name": "Carl"
},
{
"name": "Bob"
}
]
"$$sort(##current.name):$"
[
{
"name": "Alice"
},
{
"name": "Bob"
},
{
"name": "Carl"
},
{
"name": "Dan"
}
]
[
{
"name": "Dan"
},
{
"name": "Alice"
},
{
"name": "Carl"
},
{
"name": "Bob"
}
]
"$$sort(##current.name,DESC,STRING):$"
[
{
"name": "Dan"
},
{
"name": "Carl"
},
{
"name": "Bob"
},
{
"name": "Alice"
}
]
[
"A2",
"B3",
"C2",
"D4",
"B1",
"B2",
"D1"
]
{
"$$sort": "$",
"by": "$$substring(0,1):##current"
}
[
"A2",
"B3",
"B1",
"B2",
"C2",
"D4",
"D1"
]
[
"A2",
"B3",
"C2",
"D4",
"B1",
"B2",
"D1"
]
{
"$$sort": "$",
"by": "$$substring(0,1):##current",
"then": [
{
"by": "$$long:$$substring(-1):##current"
}
]
}
[
"A2",
"B1",
"B2",
"B3",
"C2",
"D1",
"D4"
]
[
"A2",
"B3",
"C2",
"D4",
"B1",
"B2",
"D1"
]
{
"$$sort": "$",
"by": "$$substring(0,1):##current",
"then": [
{
"by": "$$long:$$substring(-1):##current",
"order": "DESC"
}
]
}
[
"A2",
"B3",
"B2",
"B1",
"C2",
"D4",
"D1"
]
[
"A2",
"B3",
"C2",
"D4",
"B1",
"B2",
"D1"
]
{
"$$sort": "$",
"by": "$$substring(0,1):##current",
"order": "DESC",
"then": [
{
"by": "$$long:$$substring(-1):##current",
"order": "ASC"
}
]
}
[
"D1",
"D4",
"C2",
"B1",
"B2",
"B3",
"A2"
]
[
"A2",
"B3",
"C2",
"D4",
"B1",
"B2",
"D1"
]
{
"$$sort": "$",
"by": "$$substring(0,1):##current",
"order": "DESC",
"then": [
{
"by": "$$long:$$substring(-1):##current",
"order": "DESC"
}
]
}
[
"D4",
"D1",
"C2",
"B3",
"B2",
"B1",
"A2"
]