-1

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?

JSum
  • 597
  • 9
  • 20
  • Ok well could you give me a link or something to back up your down vote? I've spent the last 12 hours on google researching this and I haven't found anything helpful to my specific question. – JSum Jun 21 '17 at 17:40
  • `var sorted = rows.elements.sort((a, b) => +a.distance.text.split(' ')[0] - +b.distance.text.split(' ')[0]);`. Split the distance text on space, take the first element of the resulting array, convert to number via the unary `+` operator, and bam. None of that is in any way difficult to follow, and the structure is lifted straight from the mdn docs on `Array.prototype.sort` and `String.prototype.split`. – Jared Smith Jun 21 '17 at 17:49
  • [here](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) are [some](https://stackoverflow.com/questions/10003683/javascript-get-number-from-string) [different](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) [links](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) – Jared Smith Jun 21 '17 at 17:52

1 Answers1

-1
distances = rows[0].elements.map( function(item) {
    return item.distance.value;
}
distances.sort()
sorted_elements = [];
rows[0].elements.forEach( function(item, key) {
    sorted_elements[distances.indexOf( item.distance.value )] = item;
} );
rows[0].elements = sorted_elements;
delete sorted_elements;
delete distances;

First you get array of sorted distances, then you give elemets key according to distance key.

azurinko
  • 261
  • 2
  • 11