Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Lists of Custom Types and Sorting

Lists can hold objects of your user-defined types (your Apex classes). Lists of user-defined types can be sorted.

To sort such a list, your Apex class can implement the Comparator interface and pass it as a parameter to the List.sort method. Alternatively, your Apex class can implement the Comparable interface.

The sort criteria and sort order depend on the implementation that you provide for the Comparable.compareTo or the Comparator.compare method.

To perform locale-sensitive comparisons and sorting, use the Collator class. Because locale-sensitive sorting can produce different results depending on the user running the code, avoid using it in triggers or in code that expects a particular sort order.