Handlebar Helper Function: Sort

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 (API version 65.0).

Syntax 

1{{sort list fieldName sortOrder sortAs}}

Parameters 

ParameterTypeDescription
listArrayRequired. The list of objects to sort.
fieldNameStringRequired. The field to sort by. Must exist in all objects.
sortOrderStringRequired. The sort order. Valid values are "asc" (ascending order) and "desc" (descending order).
sortAsStringRequired. The type to sort as. Valid values:
  • "string"
  • "number"
  • "date"
  • "boolean"
.

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.

1myList = [{"age": 24}, {"age": 27}, {"age": 21}];
2
3{{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.

1myList = [{"name": "John"}, {"name": "Alice"}, {"name": "Bob"}];
2
3{{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.

1myList = [{"isActive": false}, {"isActive": true}];
2
3{{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.

1myList = [
2  {"orderId": "o1", "createdAt": "2024-01-10", "total": 100},
3  {"orderId": "o2", "createdAt": "2024-01-11", "total": 120},
4  {"orderId": "o3", "createdAt": "2024-01-12", "total": 150}
5];
6
7{{sort (filter myList "total" ">" 100 "number") "createdAt" "desc" "date"}}

The function returns this list:

1[
2  { "orderId": "o3", "createdAt": "2024-01-12", "total": 150 },
3  { "orderId": "o2", "createdAt": "2024-01-11", "total": 120 }
4]