# Sort a Table

### Overview

This procedure sorts a table by one or more columns. The sort order (ascending or descending) can be specified. Optionally, you can specify a list of sort orders for multiple columns.

This is useful in a variety of situations—for example, you can sorting a score or sales column, arrange items alphabetically by name or category, or sort by date to show the most recent entries first.

### Input Concepts

| Concept  | Type  | Description                                                                                                     | Default    | Required |
| -------- | ----- | --------------------------------------------------------------------------------------------------------------- | ---------- | -------- |
| `table`  | table | The table to be sorted.                                                                                         | No default | Yes      |
| `column` | text  | The column(s) by which the table should be sorted. Can be a single column name or comma-separated list.         | No default | Yes      |
| `order`  | text  | Optional sort order for single column. Format: ascending / descending column\_name                              | No default | No       |
| `orders` | text  | Optional list of sort orders for multiple columns. Format: ascending / descending column\_name for each column. | No default | No       |

### Output Concepts

| Concept | Description       |
| ------- | ----------------- |
| `table` | The sorted table. |

### Examples

For these examples, say we create a table using the following command:

```
create a table where
    the column names are "name", "age" and "address"
```

#### 1. Sort with Default Ascending Order

In this example, no order is specified, so the default sorting order is ascending.

```
sort the table by "age"
```

#### 2. Sort with Default Descending Order

In this example, the "age" column is sorted in descending order.

```
sort the table by "age" where
    the order is "descending age"
```

#### 3. Sort Multiple Columns

In this example, both the "city" and "age" columns are sorted in different orders.

```
sort the table by "city, age" where
    the orders are "descending city", "ascending age"
```
