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
IKZIKZ 

custom settings in alphabetical order

i've added sort() to my code, but values from the custom setting are still listed in random order, what am i missing?
 
public List<SelectOption> getAvailableDocumentTypes() {
        List<SelectOption> options = new List<SelectOption>();
        options.sort();
        for (Available_Document_Types__c doc_type : Available_Document_Types__c.getall().values()) {
            options.add(new SelectOption(doc_type.Value__c, doc_type.Name));
        }
        return options;
    }

 
Best Answer chosen by IKZ
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You are using sort() on a blank list. You need to use sort() after you are adding the values to the options list. 

Use this:
public List<SelectOption> getAvailableDocumentTypes() {
        List<SelectOption> options = new List<SelectOption>();
        for (Available_Document_Types__c doc_type : Available_Document_Types__c.getall().values()) {
            options.add(new SelectOption(doc_type.Value__c, doc_type.Name));
            options.sort();
        }
        return options;
    }

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Payal Popat 27Payal Popat 27
Hi, Please try below:

public List<SelectOption> getAvailableDocumentTypes() {
List<SelectOption> options = new List<SelectOption>();
for (Available_Document_Types__c doc_type : Available_Document_Types__c.getall().values()) {
options.add(new SelectOption(doc_type.Value__c, doc_type.Name));
}
options.sort();
return options;
}

Please mark the answer as best answer if it resolves your issue.

Thanks.
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You are using sort() on a blank list. You need to use sort() after you are adding the values to the options list. 

Use this:
public List<SelectOption> getAvailableDocumentTypes() {
        List<SelectOption> options = new List<SelectOption>();
        for (Available_Document_Types__c doc_type : Available_Document_Types__c.getall().values()) {
            options.add(new SelectOption(doc_type.Value__c, doc_type.Name));
            options.sort();
        }
        return options;
    }

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
IKZIKZ
thank you