# How can we help?

## Overview

Cordial provides two 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

## Use the `sort` parameter when retrieving data with a supported GET method

The [getOrders](https://support.cordial.com/hc/en-us/articles/115005693207) and [getEventRecords](https://support.cordial.com/hc/en-us/articles/115005693247) methods provide parameters to sort the data available to use in a message. More methods will be supported with upcoming releases.

You can select the column to be sorted as well as the direction (ascending or descending).

- [Learn more about sorting data with the getOrders method](https://support.cordial.com/hc/en-us/articles/115005693207).
- [Learn more about sorting data with the getEventRecords method](https://support.cordial.com/hc/en-us/articles/115005693247).

## Use 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's 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 automations and data 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"
  }
]
```

**Nissan Versa**  
$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're getting the automobile data using the [getJson method](https://support.cordial.com/hc/en-us/articles/115005857328). 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'll 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, you can [learn about the `string contains` utility](https://support.cordial.com/hc/en-us/articles/115005856548).
