$shuffle
$shuffle(array) -> arrayCompact type signature
<a:a>Source: JSONata
Documentation
Returns a shuffled copy of the input array.
FUME uses a Fisher-Yates-style shuffle, so the output contains the same members as the input but in an intentionally unpredictable order. undefined input returns undefined, and arrays of length 0 or 1 are returned unchanged.
Because the exact ordering is nondeterministic, examples should assert stable properties of the shuffled result instead of a specific permutation.
Examples
Shuffle an array without depending on a specific order
Input
This example uses the patient-summary example input. The expression shuffles the tags array, then sorts the shuffled values only for display so the rendered result stays deterministic.
Example input
JSON
{
"patientId": "pat-0001",
"name": {
"given": "Avery",
"family": "Reed",
"display": "Avery Reed"
},
"birthDate": "1990-06-12",
"sex": "female",
"primaryCareTeam": {
"organization": "ExampleCare",
"facility": "ExampleCare Clinic",
"practitioner": {
"practitionerId": "prac-4001",
"display": "Jordan Kim"
}
},
"identifiers": {
"memberId": "mbr-3001",
"recordNumber": "rec-5001"
},
"tags": [
"demo",
"fictional"
],
"_xmlTagName": "patientSummary"
}
Expression
{ "count": $count($shuffle(tags)), "members": $sort($shuffle(tags)) }
Result
{
"count": 2,
"members": [
"demo",
"fictional"
]
}