0

So say I have an array of objects that includes details about individuals. The array is as follows:

volunteers = [{name:"john",age:"32",origin:{lat:(some number),lng:(some number)},{name:"tony",age:"34",origin:{lat:(some number),lng:(some number)},{name:"timothy",age:"27",origin:{lat:(some number),lng:(some number)},{name:"pat",age:"35",origin:{lat:(some number),lng:(some number)}

These individuals are volunteer firefighters. I'd like to sort this array by their distance to my fire. The fire also has coordinates fireCoordinates: {lat:(some number),lng:(some number)}

How do I sort my volunteers array by their distance to the fire?

Edit: I've tried different variations of the Haversine formula so far but they haven't been the cleanest solutions since a relatively large amount of code is required and a tedious loop. Also, I'm not necessarily looking for a positive answer, maybe it can't be done. I'd love to understand why either ways.

Thanks

Nizar
  • 737
  • 6
  • 15
  • 1
    you could try [this](https://stackoverflow.com/questions/26836146/how-to-sort-array-items-by-longitude-latitude-distance-in-javascripts) – tpliakas Sep 12 '20 at 18:08
  • What have you tried? By now you should have at least researched how to get distance between two points – charlietfl Sep 12 '20 at 18:09
  • I've tried different variations of the Haversine formula. My problem is that its more geared towards finding a distance between 2 points rather than sorting an array using it. But also it's not a compact solution. I was looking for something a bit simpler more tidy. – Nizar Sep 12 '20 at 18:12
  • @tpliakas I've tried it. But again, its not such clean solution because its geared towards calculating the distance between 2 points. I'm looking at a potential array of 100's of individuals. – Nizar Sep 12 '20 at 18:14
  • @nizhabib Please show us the code you tried that uses the haversine formula, that appears to be the right approach. Did it not work, or did you just think it wasn't compact enough? And yes, you will need to call the distance calculating function a hundred times, once for each individual location in your array, there's no way around that. – Bergi Sep 12 '20 at 18:40

1 Answers1

0

Okay so I've managed to find a solution with node.js. I've used the geolib package. In it you'll find a function called orderByDistance(point, arrayOfPoints). point refers to your target coordinate (in my example, the location of the fire fireCoordinates). And arrayOfPoints refers an array of coordinates to sort (in my example, the volunteers array). I had to change my volunteers array a bit, placing the latitude and longitude outside of the variable origin in fields that have to be called latitude and longitude specifically (any other field names wont work). In short, in order for the orderByDistance function to work, I changed my volunteers array to look like this

volunteers = [{name:"john",age:"32",latitude:(some number),longitude:(some number)},{name:"tony",age:"34",latitude:(some number),longitude:(some number)},{name:"timothy",age:"27",lat:(some number),lng:(some number)},{name:"pat",age:"35",latitude:(some number),longi:(some number)]

Finally, using the function is easy:

volunteers = orderByDistance({ latitude: fireCoordinates.lat, longitude: fireCoordinates.lng }, volunteers);

The results will be a new volunteers array that is sorted by distance to your point coordinates. From closets to furthest.

Nizar
  • 737
  • 6
  • 15