abhinavguptas / Apex-Select-Option-Sorting

Salesforce APEX API to Sort Array/List of SelectOption instances

Home Page:http://www.tgerm.com/2011/08/salesforce-apex-selectoption-sort.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This repository provides a Utility class to provide efficient sorting of Apex SelectOption instances. 
Efficient as all the heavy lifting of sorting is internally handled by native List.sort(),
so this leads to fast sorting and lesser consumption of script statements and one is more safe from Governor. 

Example Usage 
=============

Sort by Label :
----------------
Selectoption[] opts = new Selectoption[] {
                                   new Selectoption('v8', 'l8'),
                                   new Selectoption('v1', 'l1'),
                                   new Selectoption('v5', 'l5')
                               };
       
SelectOptionSorter.doSort(opts, SelectOptionSorter.FieldToSort.Label);
// This will print the sorted list
System.debug (' Sorted Options : ' + opts);


//
// Following will be output on console
//

|DEBUG| Sorted Options : (System.SelectOption[value="l1", label="v1", disabled="false"], System.SelectOption[value="l5", label="v5", disabled="false"], System.SelectOption[value="l8", label="v8", disabled="false"])


Sort by Value :
-----------------
// Just pass SelectOptionSorter.FieldToSort as "SelectOptionSorter.FieldToSort.Value" 
// instead of "SelectOptionSorter.FieldToSort.Label"
SelectOptionSorter.doSort(opts, SelectOptionSorter.FieldToSort.Value);

About

Salesforce APEX API to Sort Array/List of SelectOption instances

http://www.tgerm.com/2011/08/salesforce-apex-selectoption-sort.html