Sorts a list of objects by a specified field, order, and type. Supports sorting as string, number, date, or boolean.
Availability
This helper is available in the Winter ’26 release of Marketing Cloud Next.
Syntax
{{sort list fieldName sortOrder sortAs}}
Parameters
| Parameter | Type | Description |
|---|---|---|
list |
Array | Required. The list of objects to sort. |
fieldName |
String | Required. The field to sort by. Must exist in all objects. |
sortOrder |
String | Required. The sort order. Valid values are "asc" (ascending order) and "desc" (descending order). |
sortAs |
String | Required. The type to sort as. Valid values: <ul><li>"string"</li><li>"number"</li><li>"date"</li><li>"boolean"</li></ul>. |
Return Value
The function returns a new list sorted by the specified field, order, and type. Objects with missing fields or invalid values for the specified type are excluded from the result. If a parameter is missing or invalid, the function throws an exception.
Usage Examples
This example shows how to use the sort function to sort a list of objects by the age field in ascending order.
myList = [{"age": 24}, {"age": 27}, {"age": 21}];
{{sort myList "age" "asc" "number"}}
The function returns [{"age": 21}, {"age": 24}, {"age": 27}].
String Sorting
When the field is a string, the function sorts the strings in alphabetical order, as shown in this example.
myList = [{"name": "John"}, {"name": "Alice"}, {"name": "Bob"}];
{{sort myList "name" "asc" "string"}}
The function returns [{"name": "Alice"}, {"name": "Bob"}, {"name": "John"}].
Boolean Sorting
When the field is a boolean, the function sorts the booleans with false before true, as shown in this example.
myList = [{"isActive": false}, {"isActive": true}];
{{sort myList "isActive" "asc" "boolean"}}
The function returns [{"isActive": false}, {"isActive: true}].
Nested Helpers
You can combine the sort function with the filter function to filter a list by one field, and then sort the resulting list by another field.
This example shows how to use the sort function with the filter function to sort a list of objects by the createdAt field in descending order, only including objects where the value of the total field is greater than 100.
myList = [
{"orderId": "o1", "createdAt": "2024-01-10", "total": 100},
{"orderId": "o2", "createdAt": "2024-01-11", "total": 120},
{"orderId": "o3", "createdAt": "2024-01-12", "total": 150}
];
{{sort (filter myList "total" ">" 100 "number") "createdAt" "desc" "date"}}
The function returns this list:
[
{ "orderId": "o3", "createdAt": "2024-01-12", "total": 150 },
{ "orderId": "o2", "createdAt": "2024-01-11", "total": 120 }
]