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.

{
  "ViewConfig": {
    "Resources": {
      "Community": {
        "Id": { "name": "communityId" },
        "Name": { "name": "communityName" },
        "Order": [
          { "Field": { "name": "communityName", "order": "ASC" } }
        ]
      }
    }
  }
}
---
ViewConfig:
  Resources:
    Community:
      Id:
        name: "communityId"
      Name:
        name: "communityName"
      Order:
      -
        Field:
          name: "communityName"
          order: "ASC"

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

{
  "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" } }
        ]
      }
    }
  }
}
---
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"

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.

For simplicity, this query has no filtering. Executing filtering would return all assets and all relations available in Collibra.

{
  "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" } }
        ]
      }
    }
  }
}
---
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"