0

I have a visualforce page where I am displaying dynamic picklist

Visualforce code:

<apex:selectList value="{!SelectedNomination}" size="1" id="a">
   <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
</apex:selectList>

Apex Code:

public List<Nominator__c> Nominators = new List<Nominator__c>();

public List<SelectOption> contactlist
{

    get
    {

        String state = 
             application.Congressional_State_Territory__c.substring(0,2);

        Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c, 
              Nominator_Code__c, Nominator_Type__c from Nominator__c WHERE 
              FirstTwoLetters__c =: state and Nominator_Type__c= 
              'Congressional - House' and Class_Year__c =: 
              application.Class_Year__c and Status__c =:'Active' ];

        contactlist = new List<SelectOption>();

        for(Nominator__c nom : Nominators)
        {
            contactlist.add(new SelectOption(nom.Id, nom.Name + ' ' + 
            nom.Contact_Name__c));
        }

        **contactlist.sort();**
        return contactlist;
    }
    set {}
} 

The data in my picklists are as "Florida District 01 Peter Scott" , "Florida District 02 Bob Stu", District 03 Matt Cauller", ..... goes onto Distrcit 14.

I have used the sort function above before displaying the picklist. It displays all other values in correct order except District 01 and 14. Their position is reversed.How do I make them display in their correct position. This is how it displays

Florida District 14 .........."

Florida District 02 .........."

................03...........

................04..........

...............05........

...............06.......

...............07......

.........................

.........................

Florida District 01 .........."

  • I tried sorting my list the way you did and mine sorted correctly, I suspect that your ......... are hiding some of the information from us, can you do something like system.debug(string.valueOf(contactlist)); After you sort it and post the debug output. – gNerb Apr 12 '18 at 21:44
4

Since you are creating the List based on query, so better to use ORDER BY clause for sorting. Here is an example.

Nominators = [Select Id, Name, Class_Year__c, Contact_Name__c, 
              Nominator_Code__c, Nominator_Type__c 
              from Nominator__c 
              WHERE 
              FirstTwoLetters__c =: state 
              and Nominator_Type__c= 'Congressional - House' 
              and Class_Year__c =: application.Class_Year__c 
              and Status__c =:'Active' 
              ORDER BY Name ASC] ;

Verify, whether you need to use ASC or DESC

  • 1
    Thank you so much. Sorting in ASC order worked for me!! Thanks again. – Student Apr 12 '18 at 21:46

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.