1
public enum FieldToSort {Label, Value}    
    public static void doSort(List<Selectoption> opts, FieldToSort sortField) {        
        Map<String, Selectoption> mapping = new Map<String, Selectoption>();        
        Integer suffix = 1;
        for (Selectoption opt : opts) {
            if (sortField == FieldToSort.Label) {
                mapping.put( (opt.getLabel() + suffix++), opt);   
            } else {
                mapping.put( (opt.getValue() + suffix++), opt);   
            }
        }        
        List<String> sortKeys = new List<String>();
        sortKeys.addAll(mapping.keySet());
        sortKeys.sort();        
        opts.clear();        
        for (String key : sortKeys) {
            opts.add(mapping.get(key));
        }
    }

This is test class

List<Selectoption> opts =new List<Selectoption>();
        opts.add(new Selectoption('abc','abc'));
        opts.add(new Selectoption('xyz','xyz'));
        enum FieldToSort {Label, Value};
        DiscoverOrg_clsBrowse.doSort(opts, FieldToSort.Label);
1

Hi You need to pass enum value by below way

        List<Selectoption> opts =new List<Selectoption>();
        opts.add(new Selectoption('abc','abc'));
        opts.add(new Selectoption('xyz','xyz'));
        FieldToSort enumValue = FieldToSort.Label;
        DiscoverOrg_clsBrowse.doSort(opts, enumValue );

or

        List<Selectoption> opts =new List<Selectoption>();
        opts.add(new Selectoption('abc','abc'));
        opts.add(new Selectoption('xyz','xyz'));
        DiscoverOrg_clsBrowse.doSort(opts, FieldToSort.Label);

This works if you are executing this code in the class where enums are defined. Otherwise you need to consider creating a separate enum class.

public  enum FieldToSort {
    Label, 
    Value
}

This will make sure that it is publicly available and not scoped to the class in which it is defined. Hope this helps you.

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.