Sorting Apex SelectOption Array/List
All Apex developers creating Visualforce pages with drop-downs and checkboxes use the Apex SelectOption class. On most occasions, instances of this class are created out of records in a database that we can query in a desired alphabetical order, for example:
“Select CountryName__c, Country_code__c from Country_Master__c Order by CountryName__c”
So here we can easily create a drop-down of countries having country names in ascending sorted order.
But, on a couple of occasions, we have to sort Selectoptions without using SOQL ordering. So custom sorting is required; unfortunately, Apex Collections API just gives sorting API on List for primitives. So I tried to come up with a simple sorting API just for SelectOption collections only. The whole API is available in a class called “SelectOptionSorter”.
SelectOptionSorted API
The core motive of this API was to give simple and efficient sorting for Apex SelectOption only. Here being efficient is very important, as sorting algorithms mostly end up taking a huge amount of script statements, and Apex Governor gives us 200,000 lines only!
So to be efficient, this API:
Uses Apex System/Native List.Sort() to get the actual sorting done.
The number of script lines is minimized to the best extent to give more script line bandwidth to client code for other stuff.
This API provides sorting support on either the Label or Value field of SelectOption, though sorting on Label makes sense most of the time, but who knows? 🙂.
Code Samples
// This is the Array of SelectOptions to be sorted
Selectoption[] opts = new Selectoption[] {
new Selectoption('v8', 'l8'),
new Selectoption('v1', 'l1'),
new Selectoption('v5', 'l5')
};
Code Sample – Sort by Label
// doSort() returns nothing, it sorts the same inbound collection
SelectOptionSorter.doSort(opts, SelectOptionSorter.FieldToSort.Label);
Code Sample – Sort by Value
// doSort() returns nothing, it sorts the same inbound collection
SelectOptionSorter.doSort(opts, SelectOptionSorter.FieldToSort.Value);
Source on GitHub.com
Here is the link to the repository: https://github.com/abhinavguptas/Apex-Select-Option-Sorting
Using Apex-Commons for the same!
One really good framework available to sort any Type is Apex-Commons, it is available on git here: https://github.com/apex-commons. This framework is a rich collection of not only sorting but many other useful APIs. If you are already using it and know how to sort custom types using it, you can use Apex-Commons very well.
My core reason for coming up with this stand-alone API was:
I want a simple to deploy and use sorting API for a common Apex system class, i.e. SelectOption. So it is just one single class with all code and test methods in it. Just copy it and use it for good.
The Apex-Common sorting algorithm is great and is very generic, like Java Collections API sorting algorithms. It is generic as it can sort any Apex Type; one just needs to create the right comparator. But this generosity comes with some cost, and its number of script lines—more details explained here.
Sorting Algo used in SelectOptionSorter API uses List.sort() internally, so all heavy lifting is done by Native Apex, and script statements are saved. So typically a list of “N” items would be sorted in “3N” script lines.