0

I have the following JS function to make an AJAX call to my PHP file which contains a geojson with 175 countries.

Each of these country names and codes are appended to my nav bar drop-down list

$(document).ready(() => {

    if ("geolocation" in navigator) {
        const geoLocation = navigator.geolocation.getCurrentPosition((position) => {

            map.panTo(new L.LatLng(position["coords"]["latitude"], position["coords"]["longitude"], ))

        });

    }
    $.ajax({
        url: "assets/php/getcountryList.php",
        type: "GET",
        dataType: "json",
        data: {},
        success: function(result) {
        console.log(result)
        for (var i = 0; i < result.data.border.features.length; i++) {
           $('#innerSelect').append("<option value=" + result.data.border.features[i].properties.iso_a2 + ">" + result.data.border.features[i].properties.name + "</option>");
        }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);

        }
    })
    addCountryBorder();
})

However, currently they're unordered. How can I list in the dropdown alphabetically?

  • The simplest solution would be to order them before returning in `getcountrylist.php`. Failing you can use `sort()` to order the elements of an array. See the duplicate for more information – Rory McCrossan Nov 04 '22 at 10:27

0 Answers0