Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
JavaScript import JSON data - Loop to sort by long/lat location

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?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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)
    );
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!