Order Results
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.
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
- PhoneNumber
- Url
- Currency
- Geolocation
- Longitude
- Latitude
- Picklist
- Long
- Double
- Percent
- Base64
Consider these guidelines for specific field types.
- Corporate currency always sorts using corporate currency value, if available.
- Phone data does not include any special formatting when sorting, for example, non-numeric characters such as dash or parentheses are included in the sorting.
- Picklist sorting is defined by the picklist sort determined during setup.
These data types are not supported.
- Multi-select picklist
- Rich text area
- Long text area
- Encrypted
- Data category group reference, if Salesforce Knowledge is enabled
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 compound fields aren't sortable.
To sort using compound fields, use its constituent fields. For example, BillingLatitude and BillingLongitude are constituent fields of the BillingAddress compound field.
Multiple column sorting is supported by listing more than one field on the orderBy
argument.
The order of results isn't guaranteed unless you use the orderBy
argument in a query. When you use orderBy
, the order of results can vary if there are duplicate values on the fields you use in the orderBy
argument.
For example, if there are multiple account records assigned to the same industry, the order of the results can vary. To ensure that the order of the results is consistent, add the Id to the orderBy
argument.
The previous query is similar to the following SOQL statement.
The following example returns Account records in alphabetical order by first name, sorted in descending order, with accounts that have null names appearing last.
The previous query is similar to the following SOQL statement.