Possible Duplicate:
How to sort an array of javascript objects?
Well, more preciselly, I have the following class:
function Location(name, latitude, longitude){
this.latitude = latitude;
this.longitude = longitude;
this.name = name;
}
And I want to sort an array of these objects, by order of proximity to a given Location (an object of a class like this one).
You’ll need a comparator function:
I don’t bother with the square roots there because I don’t think it’s necessary. Also I’m not accounting for any weirdness from spherical geometry, because again I don’t think it’s worth the complexity. However, if you have your own existing way to compute a distance, it could be plugged in instead of what I typed above.
You’d call that by just passing your array, plus the reference point coordinates, to that function. If you wanted to pass a “Location” instance instead it should be clear what to change.