I am finding all kinds of examples on how to sort an array, but none of them show how to use the array after.
I am using the google distance matrix to retrieve distance data for several destinations from one origin. I can output the destination info not problem:
$.each(response.rows[0].elements, function(x, y) {
var distance = response.rows[0].elements[x].distance;
var distance_value = distance.value;
var distance_text = distance.text;
var miles = distance_text.substring(0, distance_text.length -3);
var milesInt = parseInt(miles);
$('#result').append(destination + " - " + miles + "<br>" );
console.log(milesInt);
});
and I converted the string for miles into an int because I assumed it would be easier to sort, but it's inside the loop.
here is an example of the data returned from the api per location (there are 2 locations below and I removed address)
{
"rows": [
{
"elements": [
{
"distance": {
"text": "328 mi",
"value": 527479
},
"duration": {
"text": "4 hours 48 mins",
"value": 17299
},
"status": "OK"
},
{
"distance": {
"text": "327 mi",
"value": 525717
},
"duration": {
"text": "4 hours 54 mins",
"value": 17624
},
"status": "OK"
}
]
}
],
"originAddresses": [
"Texas, USA"
],
]
}
Can I sort by a variable inside the loop?