Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Sorting List Collector variable values

Erica2
Tera Contributor

Hello,

We have a software list collector that contains 'Other'.  The list currently sort by alphabetical order.  Is it possible to move the 'Other' value to the bottom of the list?  Thank you

 

Erica2_0-1698333205994.png

 

8 REPLIES 8

Sorry yours is a list collector, this is for drop downs.

 

I'll keep thinking and post back if I find something similar for list collector

-Anurag

Thank you so much @Anurag Tripathi  for your time and effort.

Riya Verma
Kilo Sage

Hi @Erica2 ,

 

Hope you are doing great.

 

Try using below script n pn load client script:

function onLoad() {
    // Check if the form is in the correct view and table
    if (g_form.getTableName() == 'your_table_name') {
        var listCollector = g_form.getControl('your_list_collector_field');
        
        if (listCollector) {
            // Get the options from the list collector
            var options = listCollector.options;
            
            // Custom sort function to move 'Other' to the bottom
            Array.prototype.sort.call(options, function(a, b) {
                if (a.text == 'Other') return 1;
                if (b.text == 'Other') return -1;
                return a.text.localeCompare(b.text);
            });
            
            // Refresh the list collector to apply the new sort order
            g_form.refresh();
        }
    }
}

 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Hi @Riya Verma 

I'm still facing the same issue after tried your suggestion:  Thank you

 

Erica2_0-1698353703011.png