My <apex:selectOptions> component is binding the Set<SelectOption> items in an inconsistent order each time the page is loaded. My expectation was to see the items appear in the select list in the same order in which they exist in the Set structure. What am I doing wrong, and, if this is expected behavior, how can I set a certain SelectOption to be pre-selected by default?

VF Page

    <apex:selectList id="programFilter" onchange="searchColumn(4, this.value);" size="1" style="padding: 10px;">
        <apex:selectOptions value="{!programs}" />


public class ThisClass {
    private Set<SelectOption> programs = new Set<SelectOption>();

    public ThisClass {

    private void setDefaultFilterValues() {
        this.programs.add(new SelectOption('', 'All Programs'));

    private void setRemainingFilterValues() {
        this.programs.add(new SelectOption('Second Value', 'SecondValue'));
  • 4
    Do you need to use set? Set is unordered by nature: salesforce.com/us/developer/docs/apexcode/Content/… – Mark Masterson Sep 2 '14 at 21:07
  • 1
    if you want a unique list add it to a set and use .addall to add the set to the list and sort the list – Rao Sep 2 '14 at 21:39
  • Thanks @MarkMasterson, that was it. If you add this as an answer I'll accept! – Brian Sep 3 '14 at 2:17

Set types do not have a guaranteed order, per the documentation: Sets

A set is an unordered collection. Do not rely on the order in which set results are returned. The order of objects returned by sets may change without warning.

If you need predictable ordering of the elements, use a List type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.