Order By Schema

AVAILABLE API VERSION
API v56.0 and later

Each orderable field on a record, such as on an account, gets a corresponding field on Account_OrderBy with type OrderByClause. The OrderByClause type allows you to specify ascending or descending order, as well as the order of null values.

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.

input Account_OrderBy {
  Id: OrderByClause
  Name: OrderByClause
  AnnualRevenue: OrderByClause
  Location__c: OrderByGeolocationClause
  Owner: User_OrderBy
  Parent: Account_OrderBy
}

OrderByClause Type

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

type Account {
  Id: ID
  Name: StringValue
  AnnualRevenue: CurrencyValue
  Location__c: Geolocation
}

The OrderByClause type contains order and nulls enumeration types.

input OrderByClause {
  order: ResultOrder
  nulls: NullOrder
}

ResultOrder Enum

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

# Specifies whether the results are ordered in ascending or descending order
enum ResultOrder {
  ASC
  DESC
}

NullOrder Enum

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

# Specifies the order in which nulls appear in the result set
enum NullOrder {
  FIRST
  LAST
}

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.

type Contact {
  AccountId: IDValue
  Account: Account # the 'Contact' sObject has a relationship 'Account' which points at the 'Account' object
}

input Contact_OrderBy {
  AccountId: OrderByClause
  Account: Account_OrderBy # 'Account_OrderBy" is used as the input type for the 'Account' input object field
}

input Account_OrderBy {
  # has the order by fields for Account
}

OrderByGeolocationClause Type

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

input Account_OrderBy {
  Name: OrderByClause
  Location__c: OrderByGeolocationClause
  Location__Latitude__s: OrderByClause
  Location__Longitude__s: OrderByClause
}

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

DistanceInput {
    latitude: Float
    longitude: Float
}

For more information, see Location-Based Filters.

See Also

ORDER BY statement