> For the complete documentation index, see [llms.txt](https://docs.comfortable.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.comfortable.io/sdk/javascript/example-sorting.md).

# Example: Sorting

Use the following query option to apply sorting:

### **sorting**

**Type:** `Comfortable.Sorting` \
sort query results ascending or descending by certain fields

#### Parameters

| Name             | Default  | Type   | Description                                                            |
| ---------------- | -------- | ------ | ---------------------------------------------------------------------- |
| **field API ID** |          | String | The field API ID sorting is applied to                                 |
| **direction**    | `desc`   | String | Sorting direction                                                      |
| **context**      | `fields` | String | Context can be either `meta` (meta fields) or `fields`(content fields) |

```javascript
const api = Comfortable.api('<repository-api-id>', '<api-key>');

var options = {
    sorting: new Comfortable.Sorting()
        .add('createdAt', 'asc', 'meta')
        .add('title', 'asc')
}


api.getDocuments(options)
  .then(result => {
    // futher implementation
  })
  .catch(err => {
    throw err;
  })
```
