> For the complete documentation index, see [llms.txt](https://developer.collibra.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.collibra.com/api/guides/introduction/the-output-module-query-language/sort-the-results.md).

# Sort the results

Use the `Order` clause to sort results. Just like filters, `Order` references one or more declared fields on the entity to be sorted or one of its children, or grandchildren.

Use the `ASC`, which is the default, and `DESC` constants to request ordering in ascending or descending order.

Additionally, you can specify the "nulls" field with either a value of "FIRST" or "LAST." This feature determines whether nulls appear before or after non-null values in the sort ordering. By default, null values sort as if larger than any non-null value. "NULLS FIRST" is the default for descending order, and "NULLS LAST" is the default otherwise.

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

```json
{
  "ViewConfig": {
    "Resources": {
      "Community": {
        "Id": { "name": "communityId" },
        "Name": { "name": "communityName" },
        "Order": [
          { "Field": { "name": "communityName", "order": "ASC", "nulls": "LAST" } }
        ]
      }
    }
  }
}
```

{% endtab %}

{% tab title="YAML" %}

```yaml
---
ViewConfig:
  Resources:
    Community:
      Id:
        name: "communityId"
      Name:
        name: "communityName"
      Order:
      -
        Field:
          name: "communityName"
          order: "ASC"
          nulls: "LAST"
```

{% endtab %}
{% endtabs %}

The following example shows assets ordered by the name of a related entity.

{% hint style="info" %}
The Output Module has a broader interpretation of assets, which includes roles, complex relations, and workflows. Use the `class` property to differentiate among these subclasses.
{% endhint %}

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

```json
{
  "ViewConfig": {
    "Resources": {
      "Asset": {
        "Id": { "name": "id" },
        "Signifier": { "name": "name" },
        "Relation": {
          "type": "SOURCE",
          "TargetAsset": {
            "Id": { "name": "targetRelatedAssetId" },
            "Signifier": { "name": "targetRelatedAsset" }
          }
        },
        "Order": [
          { "Field": { "name": "targetRelatedAsset", "order": "ASC" } }
        ]
      }
    }
  }
}
```

{% endtab %}

{% tab title="YAML" %}

```yaml
---
ViewConfig:
  Resources:
    Asset:
      Id:
        name: "id"
      Signifier:
        name: "name"
      Relation:
        type: "SOURCE"
        TargetAsset:
          Id:
            name: "targetRelatedAssetId"
          Signifier:
            name: "targetRelatedAsset"
      Order:
      -
        Field:
          name: "targetRelatedAsset"
          order: "ASC"
```

{% endtab %}
{% endtabs %}

The `type` property on the relation allows you to determine which relationship is used when navigating from the parent asset to the relation. In the example above, there might be more than one `targetRelatedAsset` for each source asset. The query engine orders the related target assets first and uses the first value to order the parent assets. Similar to filtering, the order clause only affects the entities on which it is set. In the example, the `targetRelatedAssets` is not sorted. To sort, you must add another ordering clause on the Relation entity.

You should not sort on the target asset node because ordering only makes sense in a collection. If an asset is the source for many relations and the relation has one target asset, you must sort the collection of relations, not the related target asset directly.

The following query example sorts both collections.

{% hint style="info" %}
For simplicity, this query has no filtering. Executing filtering would return all assets and all relations available in Collibra.
{% endhint %}

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

```json
{
  "ViewConfig": {
    "Resources": {
      "Asset": {
        "Id": { "name": "id" },
        "Signifier": { "name": "name" },
        "Relation": {
          "type": "SOURCE",
          "TargetAsset": {
            "Id": { "name": "targetRelatedAssetId" },
            "Signifier": { "name": "targetRelatedAsset" }
          },
          "Order": [
            { "Field": { "name": "targetRelatedAsset", "order": "ASC" } }
          ]
        },
        "Order": [
          { "Field": { "name": "targetRelatedAsset", "order": "ASC" } }
        ]
      }
    }
  }
}
```

{% endtab %}

{% tab title="YAML" %}

```yaml
---
ViewConfig:
  Resources:
    Asset:
      Id:
        name: "id"
      Signifier:
        name: "name"
      Relation:
        type: "SOURCE"
        TargetAsset:
          Id:
            name: "targetRelatedAssetId"
          Signifier:
            name: "targetRelatedAsset"
        Order:
        -
          Field:
            name: "targetRelatedAsset"
            order: "ASC"
      Order:
      -
        Field:
          name: "targetRelatedAsset"
          order: "ASC"
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://developer.collibra.com/api/guides/introduction/the-output-module-query-language/sort-the-results.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.
