How to sort the data using ArcQL

ArcQL is Hypi’s query language that is used to filter the data. We will look at the various clauses of ArcQL in a series of posts.

Let’s look at the SORT clause now.

  • You may sort the data in ascending or descending order using the SORT arcql statement.
  • Sorting works on integers, floats, and strings, etc.
  • Frame a query like this.
SORT a ASC
SORT b.c DESC
Here, a is the field, b is an object and c is a field on type b.
  • Hypi sorts the data in DESC order by default

We will work with the following schema.

type arcqlObject {
    str: String
}  

Let’s insert data into the arcqlObject.

mutation {
  upsert(
    values: {
      arcqlObject: [
        { str: "hypi"}
        { str: "low code" }
        { str: "easy to use backend" }
      ]
    }
  ) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01FJ9PZGEH9KYWWFEEM2V7EZ81"
      },
      {
        "id": "01FJ9PZGEKEHFBCGG9CYT47PYX"
      },
      {
        "id": "01FJ9PZGENZT79S698395MSSN8"
      }
    ]
  }
}

Let’s sort the string data from str field

{
  find(type: arcqlObject, arcql: "* SORT str ASC") {
    edges {
      node {
        ... on arcqlObject {
          str
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "str": "easy to use backend"
          },
          "cursor": "01FJ9PZGENZT79S698395MSSN8"
        },
        {
          "node": {
            "str": "hypi"
          },
          "cursor": "01FJ9PZGEH9KYWWFEEM2V7EZ81"
        },
        {
          "node": {
            "str": "low code"
          },
          "cursor": "01FJ9PZGEKEHFBCGG9CYT47PYX"
        }
      ]
    }

We may sort the hypi.id as well. hypi is an object and id is a field.

{
  find(type: arcqlObject, arcql: "* SORT hypi.id DESC") {
    edges {
      node {
        ... on arcqlObject {
          hypi {
            id
          }
          str
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "hypi": {
              "id": "01FJ9PZGENZT79S698395MSSN8"
            },
            "str": "easy to use backend"
          },
          "cursor": "01FJ9PZGENZT79S698395MSSN8"
        },
        {
          "node": {
            "hypi": {
              "id": "01FJ9PZGEKEHFBCGG9CYT47PYX"
            },
            "str": "low code"
          },
          "cursor": "01FJ9PZGEKEHFBCGG9CYT47PYX"
        },
        {
          "node": {
            "hypi": {
              "id": "01FJ9PZGEH9KYWWFEEM2V7EZ81"
            },
            "str": "hypi"
          },
          "cursor": "01FJ9PZGEH9KYWWFEEM2V7EZ81"
        }
      ]
    }
  }
}

Query Variables

query GetSortRecord($type: HypiMutationType!, $arcql: String!) {
  find(type: $type, arcql: $arcql) {
    edges {
      cursor
      node {
        ... on arcqlObject {
          hypi {
            id
          }
          str
        }
      }
    }
  }
}
#query variables
{
  "type": "arcqlObject",
  "arcql": "* SORT str ASC"
 }

Check the POSTMAN collection for the sort requests in different programming languages! Click </> and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!

Run in Postman