# sort-datatable

## Description

<mark style="color:purple;">`sort-datatable`</mark> *adaptor sorts specified columns of a datatable in either ascending (asc) or descending (desc) order*.

In ascending mode, numbers come first and then text (1,2,cow), in descending mode text comes first (cow,2,1).

## Inputs

**`data`**\
Type: `datatable`\
Required: Yes\
The datatable to be sorted.

**`column names`**\
Type: `dictionary`\
Required: Yes\
A dictionary of column names with sort direction, where the keys are the column names, and the values should be either `asc` (for ascending order) or `desc` (for descending order).

## Outputs

**`data`**\
Type: `datatable`\
A datatable with the sorted rows.

## Examples

### Example 1: Default behaviour.

#### Inputs:

`data:`

| id | code | name           |
| -- | ---- | -------------- |
| 1  | GB   | United Kingdom |
| 2  | TR   | Turkey         |
| 3  | US   | United States  |
| 4  | IND  | India          |
| 5  | IND  | India          |
| 6  | us   | United States  |

`column names:`

| KEY  | VALUE |
| ---- | ----- |
| code | desc  |

#### Outputs:

`data`:

| id | code | name           |
| -- | ---- | -------------- |
| 6  | us   | United States  |
| 3  | US   | United States  |
| 2  | TR   | Turkey         |
| 4  | IND  | India          |
| 5  | IND  | India          |
| 1  | GB   | United Kingdom |

-> Sorted datatable based on `code` column in descending order.

### Example 2: Sorting by multiple columns.

#### Inputs:

`data:`

| id | Country       | State/Province   |
| -- | ------------- | ---------------- |
| 1  | United States | West Virginia    |
| 2  | United States | Minnesota        |
| 3  | Canada        | Ontario          |
| 4  | Canada        | Quebec           |
| 5  | Canada        | British Columbia |
| 6  | United States | California       |

`column names:`

| KEY            | VALUE |
| -------------- | ----- |
| Country        | asc   |
| State/Province | asc   |

#### Outputs:

`data`:

| id | Country       | State/Province   |
| -- | ------------- | ---------------- |
| 5  | Canada        | British Columbia |
| 3  | Canada        | Ontario          |
| 4  | Canada        | Quebec           |
| 6  | United States | California       |
| 2  | United States | Minnesota        |
| 1  | United States | West Virginia    |

-> Sorted datatable based on first the `Country` and then the `State/Province` column in ascending order.

## Use case

* Place high-impact rows at the top of a data set (e.g. to be easiest to find in Microreact data table).
* Organize data for sharing in a report.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cgps.gitbook.io/data-flo/reference-guide/sort-datatable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
