Sorting the contents of a dropdown box

I have a public form and it has a dropdown box which is populated by a power automate flow that gets the "SiteCode" when the form is rendered. Please can you tell me is there so javascript code that will sort the returned values in alphanumeric order?

I have done this now using this code

// Fetch data and populate the dropdown
getSiteCode().then(data => {
    var siteCodeDropdown = fd.field("SiteCode");

    // Prepare options array
    let options = [];
    if (Array.isArray(data)) {
        // Populate and sort alphanumerically
        options = data
            .map(item => item.SiteCode)
            .sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }));
    } else if (data.SiteCode) {
        options.push(data.SiteCode);
    } else {
        console.error("Unexpected data format:", data);
    }

    // Set sorted options
    siteCodeDropdown.options = options;
}).catch(error => {
    console.error("Error fetching SiteCodes:", error);
});

1 Like