php - Sorting Array Results -


i have array produced on success event of ajax request. array obtained ajax request looks this:

array:

            [{                 "locationid": "9",                 "locationname": "employee residence",                 "locationlatitude": "34.47189",                 "locationlongitude": "-111.9896046999",             }, {                 "locationid": "40",                 "locationname": "tron utah",                 "locationlatitude": "33.964212",                 "locationlongitude": "-118.3783589999",              }, {                 "locationid": "39",                 "locationname": "tron enterprises",                 "locationlatitude": "33.735187",                 "locationlongitude": "-111.9579499999",              }] 

calculations performed on array provides list of locations , distance users current location. sort results distance; code follows:

jquery:

// success success: function (data) {     // obtain log/lat     navigator.geolocation.getcurrentposition(function(position) {         // obtain current position lat/lon         glbvar.latitude = position.coords.latitude;         glbvar.longitude = position.coords.longitude;         // console log         //console.log('lat: ' + glbvar.latitude + ' lon: ' + glbvar.longitude);         // index location return data         (var index = 0; index < data.length; index++) {             // items             var item = data[index];             // obtain location details             varlocationdetails[index] = {"location position": {"longitude": item.locationlongitude, "latitude": item.locationlatitude, "location": item.locationname}};             // each location detail             $.each(varlocationdetails[index], function (a,b) {                 // each locations distance (from users current location)                 varlocationsdistance = calculatedistance(glbvar.longitude, glbvar.latitude, b.longitude, b.latitude);                 // determine locations radius                 if (varlocationsdistance <= varlocationradius) {                     // determine distance each location                     vardistanceobject[varint] = {distance: varlocationsdistance, location: b.location};                     // sort distance                     //vardistanceobject[varint].sort(function(a,b) {                     //  return parseint(a.distance) - parseint(b.distance)                     //});                     // console log                     //console.log(vardistanceobject[varint]);                     // obtain location name/distance                     varlocation = b.location;                     vardistance = calculatedistance(glbvar.longitude, glbvar.latitude, b.longitude, b.latitude);                     // obtain function return                     functionreturn = '<li>' + varlocation + ': ' + vardistance + 'm</li>';                     // console log                     //console.log(functionreturn);                     // function return                     $('#groups').append(functionreturn);// append html                 };                 // increment                 ++varint;             });         }     }); } 

distance calculation

            // calculate distance             function calculatedistance(varlongitude, varlatitude, varlon, varlat) {                 // assign radius                 varradius = 6371;                 // obtain lon/lat values                 varlongitude = varlongitude * (math.pi / 180);                 varlatitude = varlatitude * (math.pi / 180);                 varlon = varlon * (math.pi / 180);                 varlat = varlat * (math.pi / 180);                 // 1st  x/y axis                 x0 = varlongitude * varradius * math.cos(varlatitude);                 y0 = varlatitude * varradius;                 // 2nd x/y axis                 x1 = varlon * varradius * math.cos(varlat);                 y1 = varlat * varradius;                 // calculate values                 dx = x0 - x1;                 dy = y0 - y1;                 // determine distance                 d = math.sqrt((dx * dx) + (dy * dy));                 // function return                 return math.round(d * 1000);             }; 

current results:

residence: 0m tron utah: 532559m tron enterprises: 45358m 

desired results:

residence: 0m tron enterprises: 45358m tron utah: 532559m 

the "sort distance" portion of code commented out because doesn't work. assistance correct code or provide alternative method appreciated. self taught amateur web developer.

thanks,

  1. first insert distance values data array. have data this:

        {         "locationid": "9",         "locationname": "employee residence",         "locationlatitude": "34.47189",         "locationlongitude": "-111.9896046999",         "distance": 45456     } 

    this done iterating on given data this:

    for ( var = 0; < data.length; i++ ) {     var entry = data[i];     entry.distance = calculatedistance(entry.locationlongitude, entry.locationlatitude, local_lon, local_lat); } 
  2. sort data distance property.

    var sorteddata = data.sort(function(a, b) {     return a.distance - b.distance; }); 

    you have array sorted distance

  3. iterate through sorted array , create html representation


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -