Press ESC to close

Sorting Apex SelectOption Array/List !

All apex developers creating visualforce pages with drop-downs and checkboxes use Apex SelectOption class. On most of the occasions instances of this class are created out of records in database that we can query in a desired alphabetical order for ex.

“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 algos mostly end up taking 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.

  • Number of script lines are minified to best extent, to give more script line bandwidth to client code for other stuff.

This API provides sorting support on either of Label or Value field of SelectOption, though sorting on Label makes sense most of the times, but who knows

Smile

Code Sample

// 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 repos : https://github.com/abhinavguptas/Apex-Select-Option-Sorting

Using Apex-Commons for the same !

One of a 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 really a rich collections 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 of coming 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 its just one single class with all code and test methods in it. Just copy it and use it for good.

  • Apex-Common sorting algo is great and is very generic like Java Collections API sorting algos. It is generic as it can sort any Apex Type, one just need to create a 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.

Feedback

Would love to hear back your views and comments.

Comments (2)

Leave a Reply

%d bloggers like this: