按对象键值对javascript中的数组进行排序
问题描述:
您将如何按距离对包含这些对象的数组进行排序.以便您将对象从最小距离到最大距离排序?
How would you sort this array with these objects by distance. So that you have the objects sorted from smallest distance to biggest distance ?
Object { distance=3388, duration="6 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}
Object { distance=13564, duration="12 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}
Object { distance=4046, duration="6 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}
Object { distance=11970, duration="17 mins", from="Lenchen Ave, Centurion 0046, South Africa", more...}
答
使用 Array
的sort()
方法,例如
Use Array
's sort()
method, eg
myArray.sort(function(a, b) {
return a.distance - b.distance;
});