Order By Examples

AVAILABLE API VERSION
API v56.0 and later

Order objects are used to order the result set of records. All sObjects in the schema have a corresponding order type. The order type combines the object API name with the _OrderBy suffix. For example, Account has a corresponding Account_OrderBy type.

Example: Order By Account Names

Pass in the orderBy argument to the object.

query accounts {
  uiapi {
    query {
      Account ( orderBy: {
        Name: {
            order: ASC
          }
        }) {
        edges {
          node {
            Id
            Name {
              value
            }
          }
        }
      }
    }
  }
}

The query returns the following example response.

{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Name": {
                  "value": "Alpha Dynamics"
                }
              }
            },
            {
              "node": {
                "Name": {
                  "value": "Burlington Textiles"
                }
              }
            }, // more results here
          ]
        }
      }
    }
  },
  "errors": []
}

Example: Order By Child Contacts on Accounts

Here's an example orderBy argument for contacts on account records.

query AccountWithChildrenAccounts {
  uiapi {
      query {
          Account(where: { Name: { eq: "Alpha Dynamics" } }) {
              edges {
                  node {
                      Id
                      Name {
                          value
                      }
                      Contacts ( orderBy: {
                        Name: {
                          order: ASC
                        }
                      }) {
                          edges {
                            node {
                              Name {
                                value
                              }
                            }
                          }
                      }
                  }
              }
          }
      }
  }
}

The query returns the following example response.

{
  "data": {
    "uiapi": {
      "query": {
        "Account": {
          "edges": [
            {
              "node": {
                "Id": "001RM000005ZdMGYA0",
                "Name": {
                  "value": "Alpha Dynamics"
                },
                "Contacts": {
                  "edges": [
                    {
                      "node": {
                        "Name": {
                          "value": "Amy Taylor"
                        }
                      }
                    },
                    {
                      "node": {
                        "Name": {
                          "value": "Caroline Kingsley"
                        }
                      }
                    },
                    {
                      "node": {
                        "Name": {
                          "value": "Jennifer Wu"
                        }
                      }
                    }
                  ]
                }
              }
            }
          ]
        }
      }
    }
  },
  "errors": []
}

note: Custom metadata types (types ending in __mdt) don't support ordering through parent relationships, and so they don't have any input object fields corresponding to parent relationships on the OrderBy object.