
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Sort array of points by ascending distance from a given point JavaScript
Let’s say, we have an array of objects with each object having exactly two properties, x and y that represent the coordinates of a point. We have to write a function that takes in this array and an object with x and y coordinates of a point and we have to sort the points (objects) in the array according to the distance from the given point (nearest to farthest).
The Distance Formula
It is a mathematical formula that states that the shortest distance between two points (x1, y1) and (x2, y2) in a two-dimensional plane is given by −
$S=\sqrt{((x2-x1)^2+(y2-y1)^2)}$
We will be using this formula to calculate the distance of each point from the given point and sort them according to that.
Example
const coordinates = [{x:2,y:6},{x:14,y:10},{x:7,y:10},{x:11,y:6},{x:6,y:2}]; const distance = (coor1, coor2) => { const x = coor2.x - coor1.x; const y = coor2.y - coor1.y; return Math.sqrt((x*x) + (y*y)); }; const sortByDistance = (coordinates, point) => { const sorter = (a, b) => distance(a, point) - distance(b, point); coordinates.sort(sorter); }; sortByDistance(coordinates, {x: 5, y: 4}); console.log(coordinates);
Output
The output in the console will be −
[ { x: 6, y: 2 }, { x: 2, y: 6 }, { x: 7, y: 10 }, { x: 11, y: 6 }, { x: 14, y: 10 } ]
And this is in fact the correct order as (6, 2) is nearest to (5,4), then comes (2, 6) then (7, 10) and so on.
- Related Articles
- Get n numbers from array starting from given point JavaScript
- C program to sort a given list of numbers in ascending order using Bubble sort
- How to sort an object in ascending order by the value of a key in JavaScript?
- Sort array by month-year JavaScript
- Sort by index of an array in JavaScript
- Sort nested array containing objects ascending and descending according to date in JavaScript
- Golang Program To Sort An Array In Ascending Order Using Insertion Sort
- Sort array of objects by string property value - JavaScript
- Sort Array of objects by two properties in JavaScript
- Sort array by year and month JavaScript
- How to sort Java array elements in ascending order?
- Program to sort a given linked list into ascending order in python
- Sort array of objects by string property value in JavaScript
- Sort an array of objects by multiple properties in JavaScript
- Python program to sort the elements of an array in ascending order
