ORDER BY Clause
You can specify the order in which search results are returned from a SOSL query using
the ORDER BY clause. You can also use the clause to display
empty records at the beginning or end of the results.
Use one or more ORDER BY clauses in a SOSL statement.
Syntax
ORDER BY fieldname [ASC | DESC] [NULLS [first | last]]
Syntax | Description |
---|---|
ASC or DESC | Orders the results in ascending or descending order. The default is ascending. You can have more than one ORDER BY clause. |
NULLS [first | last] | Orders null records at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. By default, null values are sorted first. |
Examples
This example orders the account names in ascending ID
order.
FIND {MyName} RETURNING Account(Name, Id ORDER BY Id)
This example, which shows multiple ORDER BY clauses, orders
contacts in ascending order by name and by account
description.
FIND {MyContactName} RETURNING Contact(Name, Id ORDER BY Name), Account(Description, Id ORDER BY Description)
This search returns account records in descending alphabetical order by name, with accounts that have null names appearing last.
FIND {MyAccountName} IN NAME FIELDS RETURNING Account(Name, Id ORDER BY Name DESC NULLS last)
This search returns custom objects that contain "San Francisco" in any field and have geolocation or address fields with locations that are within 500 miles of the latitude and longitude coordinates 37 and 122, respectively. The results are sorted in descending order by the locations’ distance from the coordinates.
FIND {San Francisco} RETURNING My_Custom_Object__c (Name, Id WHERE DISTANCE(My_Location_Field__c,GEOLOCATION(37,122),'mi') < 500 ORDER BY DISTANCE(My_Location_Field__c,GEOLOCATION(37,122),'mi') DESC)