JSON Data Import JavaScript - Sorting Loop by Length / Lat

I am reading a json file that looks like this:

[
    {
        "name":"Mark Brown",
        "location":{
            "lat":50.72423653147234,
            "lon":-0.6552093628538713
            }
    },

    {
        "name":"Nina Wright",
        "location":{
            "lat":51.00351657212984,
            "lon":0.4104543851588729
        }
    }

]

      

Then I have a loop that will display the data in a list box on my page.

$.getJSON(JsonFile, function(List){

  var div = document.getElementById('list');

  for( var i = 0; i < List.length;i++ ) { 

      div.innerHTML = div.innerHTML + '<li>'+List[i].name+'</li>';

  }


});

      

This will add the list to <ul id="List"></ul>

This all works great, but I need to order by location in DESC order before adding data to my page.

How can i do this?

+3


source to share


1 answer


You need a sort callback with distance deltas.



data.sort(function (a, b) {
    return (
        checkDistance(fixedlat, fixedlon, a.location.lat, a‌​.location.lon) -
        checkDistance(fixedlat, fixedlon, b.location.lat, b‌​.location.lon)
    );
});

      

0


source







All Articles