---
title: "Sorting"
description: "API documentation explaining how to specify custom sorting of query results using the orderBy parameter in GraphQL queries."
lang: en
languages:
  en: https://docs.vismasoftware.no/businessnxtapi/apireference/features/sorting/index.md
lastmod: 2026-07-10
---

> Documentation index: https://docs.vismasoftware.no/businessnxtapi/apireference/features/llms.txt


# Sorting

Last modified July 10, 2026

> API documentation explaining how to specify custom sorting of query results using the orderBy parameter in GraphQL queries.


For every query, you can specify a sorting expression through the `orderBy` parameter on each connection. Every connection has the `orderBy` parameter, although its type differs for each connection. The name format is `OrderBy_<tablename>`. Therefore, the sort order types have names such as `OrderBy_Associate` or `OrderBy_GeneralLedgerAccount`. This sort order type contains fields for all the columns of the table, except for the in-memory ones which cannot be searched or sorted. The type of all these fields is called `SortOrder` and is an enumeration with two values: `ASC` for asceding order and `DESC` for descending order. Therefore, this makes it possible to sort the results ascending or desceding by any column in the table.

The following image shows a partial view of the `OrderBy_Associate` type of the `orderBy` parameter of the `UseCompany_Associate_Connection` connection.

![Sort order type](https://docs.vismasoftware.no/businessnxtapi/apireference/features/sort1.png)

Here is an example of a query for the first 10 customers from the associates table sorted descending by their name.

```graphql { title = "Query" }
query read($cid : Int!)
{
  useCompany(no: $cid)
  {
    associate(first: 10,
              filter:{customerNo:{_not_eq:0}},
              orderBy:[{name:DESC}])
    {
      totalCount
      items
      {
        associateNo
        customerNo
        name
      }
    }
  }
}
```

```graphql { title = "Result" }
{
  "data": {
    "useCompany": {
      "associate": {
        "totalCount": 319,
        "items": [
          {
            "associateNo": 286,
            "customerNo": 10285,
            "name": "Yggdrasil Ltd."
          },
          {
            "associateNo": 12,
            "customerNo": 10011,
            "name": "Wilbur Andersen"
          },
          {
            "associateNo": 285,
            "customerNo": 10284,
            "name": "Werner Systems AS"
          },
          ...
        ]
      }
    }
  }
}
```

As you can see, the `orderBy` is an array of values which makes it possible to define multiple sorting columns. They are considered in the give order.

```graphql { title = "Query" }
query read($cid : Int!)
{
  useCompany(no: $cid)
  {
    postalAddress(
      first :5,
      orderBy: [
        {postalArea:ASC},
        {postCode:ASC}
      ]
    )
    {
      totalCount
      items
      {
        postCode
        postalArea
      }
    }
  }
}
```

```graphql { title = "Result" }
{
  "data": {
    "useCompany": {
      "postalAddress": {
        "totalCount": 53,
        "items": [
          {
            "postCode": "0001",
            "postalArea": "Bergen"
          },
          {
            "postCode": "0010",
            "postalArea": "Bergen"
          },
          {
            "postCode": "0015",
            "postalArea": "Bergen"
          },
          {
            "postCode": "0018",
            "postalArea": "Oslo"
          },
          {
            "postCode": "0021",
            "postalArea": "Oslo"
          }
        ]
      }
    }
  }
}
```

The sort order can also be passed as a variable. Here is an example:

```graphql { title = "Query" }
query read($cid : Int!,
           $size : Int!,
           $order : [OrderBy_PostalAddress]!)
{
  useCompany(no: $cid)
  {
    postalAddress(
      first :$size,
      orderBy: $order)
    {
      totalCount
      items
      {
        postCode
        postalArea
      }
    }
  }
}
```

```graphql { title = "Variables" }
{
  "cid": 9112233,
  "size": 10,
  "order": [
    {"postalArea" : "ASC"},
    {"postCode" :"ASC"}
  ]
}
```

## Deprecated sorting

The sorting order can be specified with the `sortOrder` argument, is similar to `orderBy`, except that it is a single object instead of an array.

> [!WARNING]
>
> The `sortOrder` argument is deprecated and will be removed in the future. Therefore, it is recommended to use the `orderBy` argument instead.

When you need to specify more than one column for sorting, the only way to do it when using `sortOrder` is by using the composition field `_thenBy`. This is exemplified in the next query, where we fetch the first 10 postal addresses, sorted first by postal area, asceding, and then by postal code, also ascending.

```graphql { title = "Query" }
query read($cid : Int!)
{
  useCompany(no: $cid)
  {
    postalAddress(
      first :5,
      sortOrder:{
         postalArea:ASC
         _thenBy : {
            postCode:ASC}})
    {
      totalCount
      items
      {
        postCode
        postalArea
      }
    }
  }
}
```

```graphql { title = "Result" }
{
  "data": {
    "useCompany": {
      "postalAddress": {
        "totalCount": 53,
        "items": [
          {
            "postCode": "0001",
            "postalArea": "Bergen"
          },
          {
            "postCode": "0010",
            "postalArea": "Bergen"
          },
          {
            "postCode": "0015",
            "postalArea": "Bergen"
          },
          {
            "postCode": "0018",
            "postalArea": "Oslo"
          },
          {
            "postCode": "0021",
            "postalArea": "Oslo"
          }
        ]
      }
    }
  }
}
```

Any number or fields can be chained to define the sort order in this manner. However, only one table column per level can be specified without the `_thenBy` field. Otherwise, an error message is returned and the result wil be sorted in the alphabetical order of the specified columns.

```graphql { title = "Query" }
query read($cid: Int!)
{
  useCompany(no :$cid)
  {
    postalAddress(
      first : 3,
      sortOrder:
      {
        postalArea:ASC
        postCode:ASC
      })
    {
      totalCount
      items
      {
        postCode
        postalArea
      }
    }
  }
}
```

```graphql { title = "Result" }
{
  "errors": [
    {
      "message": "More than one field is specified in one sort clause (postCode,postalArea). The result may be incorrect. Use the _thenBy field to chain multiple table fields in the sort order."
    }
  ],
  "data": {
    "useCompany": {
      "postalAddress": {
        "totalCount": 5093,
        "items": [
          {
            "postCode": "0001",
            "postalArea": "OSLO"
          },
          {
            "postCode": "0010",
            "postalArea": "OSLO"
          },
          {
            "postCode": "0015",
            "postalArea": "OSLO"
          }
        ]
      }
    }
  }
}
```


---

[View this page](https://docs.vismasoftware.no/businessnxtapi/apireference/features/sorting/)
