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
<!-- Marketing Cloud Next test script for the sort helper. Paste it into the HTML component of a
     Next email and open the preview. Each line prints the result Next gave when the call
     was rendered on its own (expect) next to the result of this run (got).
     Assembled from single-call test emails (dates on the page); it has not been
     run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[gated] got=[{{#if false}}{{sort probeList "age" "asc" "number"}}{{/if}}gated]</p>
<p>c2 expect=[3|[Bob][Ann][Cid]] got=[%%[ SET @rows = BuildRowsetFromJSON('[{"name":"Bob","age":27},{"name":"Ann","age":21},{"name":"Cid","age":24}]', '$[*]', 1) ]%%{{length rows}}|{{#each rows}}[{{name}}]{{/each}}]</p>
<p>c3 expect=[[Ann][Cid][Bob]] got=[%%[ SET @rows = BuildRowsetFromJSON('[{"name":"Bob","age":27},{"name":"Ann","age":21},{"name":"Cid","age":24}]', '$[*]', 1) ]%%{{#each (sort rows "age" "asc" "number")}}[{{name}}]{{/each}}]</p>

Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
     would stop the whole preview or block the save. -->

<!-- email 1: expect that the email saves, but the preview fails (HTTP 500) -->
{{#each (sort (buildRowSetFromString "b|a|c" "|") "" "asc" "string")}}[{{this}}]{{/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.

See also