Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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...}
share|improve this question
 
Duplicate: stackoverflow.com/questions/5421253/… –  Jonas Pegerfalk Oct 25 '11 at 12:13
add comment

1 Answer

up vote 5 down vote accepted

Use Array's sort() method, eg

myArray.sort(function(a, b) {
    return a.distance - b.distance;
});
share|improve this answer
 
+1 But it's actually not a property of the Array constructor. –  pimvdb Oct 25 '11 at 12:10
 
@pimvdb Was only meant to illustrate it's a method of Array. Better now? –  Phil Oct 25 '11 at 12:12
1  
thanks, thats nice and simple, i had a longer method doing the same. –  Harry Oct 25 '11 at 12:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.