function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adamproadampro 

Sorting List of SelectOptions

Hi,

 

I'm populating a list of all of the different objects in a respective org and returning those names to a selectList. When I retrieve the list of all the objects, they are in a random, non-alphabetical order so when they appear on the VFP it's hard to find what you're looking for. I want to sort the list of SelectOptions in alphabetical order but I can't use the sort() method on the list because it doesn't support SelectOptions.

 

Is there some kind of function for organizing a list of SelectOptions?

Cory CowgillCory Cowgill

If you can't use ORDER BY clause on your SOQL, you can do the following:

 

1. Build a List<String> and put the Names in that list. Sort this list.

2. Build a Map<String,SelectOption> using Name as the key and place SelectOption as value.

3. Instantiate a new List<SelectOption> which will be your sorted list.

4. Iterate over the list from step 1, pulling in the SelectOption from step 2, and place each one in the new list from step 3.

 

Following that process should give you sorted List<SelectOption> without too much pain.

ShikibuShikibu