JSFiddle - React, Tailwind, and code Playground
JavaScript
function Location ( name, latitude, longitude ) {
this.latitude = latitude;
this.longitude = longitude;
this.name = name;
}
Location.distance = function ( loc1, loc2 ) {
return Math.sqrt(
Math.pow( loc2.longitude - loc1.longitude, 2 ) +
Math.pow( loc2.latitude - loc1.latitude, 2 )
);
};
Location.prototype.sortByProximity = function ( arr ) {
var that = this;
arr.sort(function ( a, b ) {
return Location.distance( that, a ) - Location.distance( that, b );
});
};
var loc1 = new Location( 'foo', 1, 1 );
var loc2 = new Location( 'bar', 4, 5 );
var loc3 = new Location( 'baz', 1, 4 );
var distance12 = Location.distance( loc1, loc2 );
console.log( distance12 ); // 5 expected
var locArr = [ loc2, loc3 ];
console.log( locArr );
loc1.sortByProximity( locArr );
console.log( locArr ); // elems expeted to have switched places