Cordial provides 2 ways to sort an array of data:
- Using the "sort" parameter when retrieving data with a supported "get" method
- Using the sortArray utility to sort an array within the message
Using the "sort" parameter when retrieving data with a supported "get" method
The getOrders and getEventRecords methods provide parameters to sort the data available to use in a message. More methods will be supported with upcoming releases.
You are able to select the column to be sorted as well as the direction (ascending or descending).
Learn more about sorting data with the getOrders method.
Learn more about sorting data with the getEventRecords method.
Using the sortArray utility to sort an array within the message
Cordial provides a custom Smarty utility to sort an array of data in a message.
{$sorted_array = $utils->sortArray($arrayName, 'column_name', 'asc')}
This is useful if you need to sort an array of data after it is retrieved using a "get" method.
The example below shows the sortArray
utility in conjunction with the getJson
method to sort the list of cars by "price".
- Used in:
Message AutomationsData Automations
- Syntax:
{$utils->sortArray($arrayName, $column_name, $sort)}
{$json_feed.data = $utils->getJson('http://p5.zdassets.com/hc/theme_assets/656597/200049057/automobiles.json')} {$sorted = $utils->sortArray($json_feed.data, 'price', 'asc')}
{foreach $sorted as $car} <strong>{$car.brand|capitalize} {$car.model|capitalize}</strong> ${$car.price}<br> Mileage: {$car.mileage}<br> ID: {$car.id}<br><br> {/foreach}
[ { "model": "prius", "brand": "toyota", "price": 27000, "mileage": 66000, "id": "03" }, { "model": "versa", "brand": "nissan", "price": 16000, "mileage": 94000, "id": "05" }, { "model": "accord", "brand": "honda", "price": 24000, "mileage": 49000, "id": "07" } ]
$16000
Mileage: 94000
ID: 05
Honda Accord
$24000
Mileage: 49000
ID: 07
Toyota Prius
$27000
Mileage: 66000
ID: 03
Parameter | Description | Required | Expected | Default |
---|---|---|---|---|
arrayName | Name of the array to be sorted. | required | String | NA |
column_name | Data column name by which to sort. | required | String | NA |
sort | Sort the order of the records returned. ASC (ascending) or DESC (descending). | optional | Sort direction | ASC |
In the example above we are getting the automobile data using the getJson method. You can use this same data in your account for testing.
{$cars=$supplements->getJson('http://p5.zdassets.com/hc/theme_assets/656597/200049057/automobiles.json')}
We then loop through the data and create a new temporary array.
{foreach $cars as $car} {$tempArray[] = [ 'model' => {$car.model}, 'brand' => {$car.brand}, 'price' => {$car.price}, 'mileage' => {$car.mileage}, 'id' => {$car.id} ]} {/foreach}
Now we add the sortArray utility to sort the data by the desired column. Is this example we are sorting by price in an ascending (asc) order. We could also sort in a descending order (desc).
{$sorted = $utils->sortArray($tempArray, 'price', 'asc')}
Finally we loop through the sorted array and render the results in the message.
{foreach $sorted as $car}
<strong>{$car.brand|capitalize} {$car.model|capitalize}</strong><br>
${$car.price}<br>
Mileage: {$car.mileage}<br>
ID: {$car.id}<br> <br>
{/foreach}
In the next article learn about the "string contains" utility.
Comments
0 comments
Please sign in to leave a comment.