Handlebars sort
Handlebars sort: sorts a list of objects by a field, order, and type.
Next
Runtime verified
Test scripts included
Sorts a list of objects by a field, order, and type.
Syntax
{{sort list fieldName sortOrder sortAs}}
Returns array.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
list |
array | Yes | List of objects to order. |
fieldName |
string | Yes | Field to order by. |
sortOrder |
string | Yes | Sort order: asc or desc. |
sortAs |
string | Yes | Type to sort as: string, number, date, or boolean. |
Example
Tested in a Marketing Cloud Next email on 2026-11-25, alone in the email, with a list of objects built in AMPscript (Handlebars reads it as variable rows):
%%[ SET @rows = BuildRowsetFromJSON('[{"name":"Bob","age":27},{"name":"Ann","age":21},{"name":"Cid","age":24}]', '$[*]', 1) ]%%{{#each (sort rows "age" "asc" "number")}}[{{name}}]{{/each}}renders[Ann][Cid][Bob], as documented.- A list of plain strings with an empty field name,
{{sort (buildRowSetFromString "b|a|c" "|") "" "asc" "string"}}, fails the email preview with HTTP 500. The docs describe lists of objects only.
Show Next test script
Show calls that fail in Next (one email each)
Marketing Cloud Next
Works as documented
Sorting a list of objects renders the documented result. Tested 2026-11-25 by rendering in a Marketing Cloud Next email.