# Sorting

## Order response by creation date

Query a `collection` and order the results ascending by creation date.

{% tabs %}
{% tab title="JavaScript" %}
{% code title="GET" %}

```javascript
// Define query
var query = {
  "sorting": {
    "meta.createdAt": "ASC"
  }
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Submit the query
window.fetch(`https://api.cmft.io/v1/<repo>/<collection>?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endcode %}
{% endtab %}

{% tab title="CURL" %}

```bash
curl -g 'https://api.cmft.io/v1/<repo>/<collection>?sorting[meta.createdAt]=ASC' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}
{% code title="" %}

```http
https://api.cmft.io/v1/<repo>/<collection>?key=<apiKey>&sorting[meta.createdAt]=ASC
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Order response by field value

Query the `documents` endpoint and order the results ascending by title.

{% tabs %}
{% tab title="JavaScript" %}
{% code title="" %}

```javascript
// Define query
var query = {
  "sorting": {
    "fields.title": "ASC"
  }
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Submit the query
window.fetch(`https://api.cmft.io/v1/<repo>/documents?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endcode %}
{% endtab %}

{% tab title="CURL" %}

```bash
curl -g 'https://api.cmft.io/v1/<repo>/documents?sorting[fields.title]=ASC' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}
{% code title="" %}

```http
https://api.cmft.io/v1/<repo>/documents?key=<apiKey>&sorting[fields.title]=ASC
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Sample Request

<mark style="color:blue;">`GET`</mark> `https://api.cmft.io/v1/demo/documents?sorting[fields.title]=ASC&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck`

#### Query Parameters

| Name    | Type   | Description |
| ------- | ------ | ----------- |
| key     | string | API Key     |
| sorting | object |             |

{% tabs %}
{% tab title="200 " %}

```javascript
{
  "status": 200,
  "meta": {
    "count": 5,
    "limit": 25,
    "offset": 0,
    "total": 5
  },
  "data": [
    {
      "fields": {
        "title": "Simple Blog Post",
        "date": "2018-07-04T22:00:00.000Z",
        "text": {
          "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
          "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
        },
        "images": [
          {
            "meta": {
              "id": "968393848009134080",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [
          {
            "meta": {
              "id": "968390655497867264",
              "contentType": "news"
            }
          }
        ],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    },
    {
      "fields": {
        "title": "Nice Bicycle",
        "description": {
          "html": "<p>Aluminium Frame; Disc Brakes; Shimano XT;</p>",
          "plain": "Aluminium Frame; Disc Brakes; Shimano XT;"
        },
        "price": 1399,
        "relatedProducts": [],
        "rating": "3",
        "inStock": false,
        "images": [
          {
            "meta": {
              "id": "969465200912769024",
              "contentType": "_asset"
            }
          }
        ]
      },
      "meta": {
        "id": "969468642649575424",
        "contentType": "products",
        "repository": "968388392691830784",
        "revision": 2,
        "tags": [],
        "createdAt": "2018-07-09T12:28:56.000Z",
        "updatedAt": "2018-07-09T12:29:23.000Z"
      }
    },
    {
      "fields": {
        "title": "Hello World!",
        "date": "2018-07-06T13:00:00.000Z",
        "text": {
          "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
          "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
        },
        "images": [
          {
            "meta": {
              "id": "968393840635547648",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "fields": {
        "title": "Christian Schwerdt",
        "shorthand": "CS"
      },
      "meta": {
        "id": "968388973430968320",
        "contentType": "author",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T12:58:43.000Z",
        "updatedAt": "2018-07-06T12:58:43.000Z"
      }
    },
    {
      "fields": {
        "title": "Beautiful Bicycle",
        "description": {
          "html": "<p>Aluminium Frame; Disc Brakes; Shimano XTR;</p>",
          "plain": "Aluminium Frame; Disc Brakes; Shimano XTR;"
        },
        "price": 1399,
        "relatedProducts": [],
        "rating": "5",
        "inStock": true,
        "images": [
          {
            "meta": {
              "id": "969465200912769024",
              "contentType": "_asset"
            }
          }
        ]
      },
      "meta": {
        "id": "969464870959452160",
        "contentType": "products",
        "repository": "968388392691830784",
        "revision": 4,
        "tags": [],
        "createdAt": "2018-07-09T12:13:57.000Z",
        "updatedAt": "2018-07-09T12:30:01.000Z"
      }
    }
  ]
}
```

{% endtab %}
{% endtabs %}

## Using multiple sorting parameters

You can apply multiple order parameters:

{% tabs %}
{% tab title="JavaScript" %}

```javascript
// Define query
var query = {
  "sorting": {
    "fields.title": 'ASC',
    "meta.createdAt": 'DESC'
  }
};

// Stringify and URI Encode the Object
query = encodeURIComponent(JSON.stringify(query));

// Submit the query
window.fetch(`https://api.cmft.io/v1/<repo>/documents?query=${query}`, {
  method: 'get',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '<apiKey>'
  }
})
  .then(function (response) { return response.json() })
  .then(function (data) {
    console.log(data)
  })
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -g 'https://api.cmft.io/v1/<repo>/documents?sorting[fields.title]=ASC&sorting[meta.createdAt]=DESC' \
  -H 'Authorization: <apiKey>'
```

{% endtab %}

{% tab title="URL" %}

```http
https://api.cmft.io/v1/<repo>/documents?key=<apiKey>&sorting[fields.title]=ASC&sorting[meta.createdAt]=DESC
```

{% endtab %}
{% endtabs %}

## Sample Request

<mark style="color:blue;">`GET`</mark> `https://api.cmft.io/v1/demo/documents?sorting[fields.title]=ASC&sorting[meta.createdAt]=DESC&key=8MIO994Ley6bqyAlQAHqutiDh4g5Heck`

#### Query Parameters

| Name    | Type   | Description |
| ------- | ------ | ----------- |
| key     | string | API Key     |
| sorting | object |             |

{% tabs %}
{% tab title="200 " %}

```javascript
{
  "status": 200,
  "meta": {
    "count": 5,
    "limit": 25,
    "offset": 0,
    "total": 5
  },
  "data": [
    {
      "fields": {
        "title": "Simple Blog Post",
        "date": "2018-07-04T22:00:00.000Z",
        "text": {
          "html": "<h1>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</h1>",
          "plain": "LOREM IPSUM DOLOR SIT AMET, CONSETETUR SADIPSCING ELITR, SED DIAM NONUMY EIRMOD\nTEMPOR INVIDUNT UT LABORE ET DOLORE MAGNA ALIQUYAM ERAT, SED DIAM VOLUPTUA. AT\nVERO EOS ET ACCUSAM ET JUSTO DUO DOLORES ET EA REBUM."
        },
        "images": [
          {
            "meta": {
              "id": "968393848009134080",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [
          {
            "meta": {
              "id": "968390655497867264",
              "contentType": "news"
            }
          }
        ],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968395804664537088",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T13:25:51.000Z",
        "updatedAt": "2018-07-06T13:25:51.000Z"
      }
    },
    {
      "fields": {
        "title": "Nice Bicycle",
        "description": {
          "html": "<p>Aluminium Frame; Disc Brakes; Shimano XT;</p>",
          "plain": "Aluminium Frame; Disc Brakes; Shimano XT;"
        },
        "price": 1399,
        "relatedProducts": [],
        "rating": "3",
        "inStock": false,
        "images": [
          {
            "meta": {
              "id": "969465200912769024",
              "contentType": "_asset"
            }
          }
        ]
      },
      "meta": {
        "id": "969468642649575424",
        "contentType": "products",
        "repository": "968388392691830784",
        "revision": 2,
        "tags": [],
        "createdAt": "2018-07-09T12:28:56.000Z",
        "updatedAt": "2018-07-09T12:29:23.000Z"
      }
    },
    {
      "fields": {
        "title": "Hello World!",
        "date": "2018-07-06T13:00:00.000Z",
        "text": {
          "html": "<p>Lorem ipsum dolor sit amet, consetetur <strong>sadipscing elitr, sed diam</strong> nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>",
          "plain": "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod\ntempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At\nvero eos et accusam et justo duo dolores et ea rebum."
        },
        "images": [
          {
            "meta": {
              "id": "968393840635547648",
              "contentType": "_asset"
            }
          }
        ],
        "relatedNews": [],
        "author": [
          {
            "meta": {
              "id": "968388973430968320",
              "contentType": "author"
            }
          }
        ]
      },
      "meta": {
        "id": "968390655497867264",
        "contentType": "news",
        "repository": "968388392691830784",
        "revision": 5,
        "tags": [],
        "createdAt": "2018-07-06T13:05:24.000Z",
        "updatedAt": "2018-07-06T13:23:08.000Z"
      }
    },
    {
      "fields": {
        "title": "Christian Schwerdt",
        "shorthand": "CS"
      },
      "meta": {
        "id": "968388973430968320",
        "contentType": "author",
        "repository": "968388392691830784",
        "revision": 1,
        "tags": [],
        "createdAt": "2018-07-06T12:58:43.000Z",
        "updatedAt": "2018-07-06T12:58:43.000Z"
      }
    },
    {
      "fields": {
        "title": "Beautiful Bicycle",
        "description": {
          "html": "<p>Aluminium Frame; Disc Brakes; Shimano XTR;</p>",
          "plain": "Aluminium Frame; Disc Brakes; Shimano XTR;"
        },
        "price": 1399,
        "relatedProducts": [],
        "rating": "5",
        "inStock": true,
        "images": [
          {
            "meta": {
              "id": "969465200912769024",
              "contentType": "_asset"
            }
          }
        ]
      },
      "meta": {
        "id": "969464870959452160",
        "contentType": "products",
        "repository": "968388392691830784",
        "revision": 4,
        "tags": [],
        "createdAt": "2018-07-09T12:13:57.000Z",
        "updatedAt": "2018-07-09T12:30:01.000Z"
      }
    }
  ]
}
```

{% endtab %}
{% endtabs %}


---

# 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://docs.comfortable.io/apis/rest-api/examples/sorting.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.
