ORDER BY:
You can use ORDER BY in a SELECT statement to control the order of the query results. There is no guarantee of the order of results unless you use an ORDER BY clause in a query.
Syntax:
SELECT fieldList FROM objectType [WHERE conditionExpression] [ORDER BY fieldExpression ASC | DESC ? NULLS FIRST | LAST ?]
Above OREDER BY Expression
ASC or DESC: Specifies whether the results are ordered in ascending (ASC) or descending (DESC) order. Default order is ascending.
NULLS FIRSTS OR NULLS LAST: Orders null records at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. By default, null values are sorted first.
Example:
The following query returns a query result with Account records in alphabetical order by first name, sorted in descending order, with accounts that have null names appearing last.
Before Using ORDER BY go through this points:
1. Sorting is case insensitive.
2. ORDER BY is compatible with relationship query syntax.
3. Multiple column sorting is supported, by listing more than one fieldExpression clause.
4. Sort order is determined by current user locale. For English locales, Salesforce uses a sorting mechanism based on the UTF-8 values of the character data. For Asian locales, Salesforce uses a linguistic sorting mechanism based on the ISO 14651 and Unicode 3.2 standards.
ORDER BY Limits:
1. These data types are not supported: multi-select picklist, rich text area, long text area, encrypted (if enabled), and data category group reference (if Salesforce Knowledge is enabled).
ASC or DESC: Specifies whether the results are ordered in ascending (ASC) or descending (DESC) order. Default order is ascending.
NULLS FIRSTS OR NULLS LAST: Orders null records at the beginning (NULLS FIRST) or end (NULLS LAST) of the results. By default, null values are sorted first.
Example:
The following query returns a query result with Account records in alphabetical order by first name, sorted in descending order, with accounts that have null names appearing last.
SELECT Name FROM Account ORDER BY Name DESC NULLS LAST
Before Using ORDER BY go through this points:
1. Sorting is case insensitive.
2. ORDER BY is compatible with relationship query syntax.
3. Multiple column sorting is supported, by listing more than one fieldExpression clause.
4. Sort order is determined by current user locale. For English locales, Salesforce uses a sorting mechanism based on the UTF-8 values of the character data. For Asian locales, Salesforce uses a linguistic sorting mechanism based on the ISO 14651 and Unicode 3.2 standards.
ORDER BY Limits:
1. These data types are not supported: multi-select picklist, rich text area, long text area, encrypted (if enabled), and data category group reference (if Salesforce Knowledge is enabled).
2. 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.
3. Picklist sorting is defined by the picklist sort determined during setup.