Order Results

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 created. The order type combines the object API name with the _OrderBy suffix. For example, Account has a corresponding Account_OrderBy type created.

Fields that have a scalar or Field Value Type that support ordering have a corresponding Input Object Field of type OrderByClause. The following field types support ordering.

  • Int
  • String
  • Boolean
  • Id
  • DateTime
  • Time
  • Date
  • TextArea
  • LongTextArea
  • PhoneNumber
  • Email
  • Url
  • Currency
  • Geolocation
  • Longitude
  • Latitude
  • Picklist
  • Long
  • Double
  • Percent
  • Base64

To identify sortable fields on an object, query the objectInfos type.

Not all sortable fields can be ordered. Not all fields returned by objectInfos are sortable. For example, Address type fields aren't sortable.

Compound fields like Address isn't currently converted to a GraphQL type. To sort using compound fields, use its constituent fields. For example, BillingLatitude and BillingLongitude are constituent fields of the BillingAddress compound field.

Each orderable field on Account gets a corresponding field on Account_OrderBy with type OrderByClause. The OrderByClause type allows you to specify ascending or descending order, as well which order null values appear in.

Orderable fields are linked to InputObject types like OrderByClause, depending on their type, such as User_OrderBy for user fields, or OrderByGeolocation for geolocation fields.

Let's take a look at an example account object that demonstrates how fields map to Account_OrderBy.

The OrderByClause type contains order and nulls enumeration types.

The ResultOrder enumeration type specifies whether the results are ordered in ascending or descending order. By default, the result is ordered in ascending order.

The NullOrder enumeration type specifies the order in which nulls appear in the result set. By default, null values are sorted first.

The order by object has input object fields for each parent relationship the object has. The field has the same name as the parent relationship, and have the OrderBy type corresponding to the object that the relationship points at.

Geolocation fields map to the OrderByGeolocationClause type. Its constituent fields Location__Latitude__s and Location__Longitude__s map to the OrderByClause type.

The DistanceInput type specifies the latitude and longitude fields on a geolocation field.

For more information, see Location-Based Filters.

Pass in the orderBy argument to the object.

The query returns the following example response.

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

The query returns the following example response.

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.

ORDER BY statement