I am reading a json file which 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 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 append the list to the <ul id="List"></ul>
This all works great but what I need to do is to order the data by location in DESC order before appending the data to my page.
How can I do this?
You need a sorting callback with the delta of the distance.
data.sort(function (a, b) {
return (
checkDistance(fixedlat, fixedlon, a.location.lat, a.location.lon) -
checkDistance(fixedlat, fixedlon, b.location.lat, b.location.lon)
);
});