I can sort a simple list using the sort
method See:
var distances = [15, 18, 27, 29, 5, 3]
distances.sort(function(a, b){
return a-b;
});
console.log(distances);
But I would like to sort a list of objects based on the distance
attribute, for example:
var listDistances = [
{distance: 857, location: {lat: 15.246, lnt:16.4552}},
{distance: 26, location: {lat: 18.246, lnt:16.4552}},
{distance: 740, location: {lat: 15.246, lnt:16.4552}}
];
In this way the result would be:
var listDistances = [
{distance: 26, location: {lat: 18.246, lnt:16.4552}},
{distance: 740, location: {lat: 15.246, lnt:16.4552}},
{distance: 857, location: {lat: 15.246, lnt:16.4552}},
];
How to sort object vector by checking attribute distance
?