title: Sort an object array by euclidean distance
body: <p>I'm trying to sorte an object array by distance. Each object has a <code>x</code> and <code>y</code> coordinate and I want to sort the array so I have the next index is the closest point to <code>index - 1</code></p>
  
  <p>To do that, I created the following functions:</p>
  
  <pre><code>function compare(a, b) {
          dist_yy = (b.bounds.y) - (a.bounds.y);
          dist_xx = (b.bounds.x) - (a.bounds.x);
          dist = Math.sqrt(dist_yy*dist_yy + dist_xx*dist_xx); 
          console.log("distance " + dist)
          return dist;
      }
  
  function sort_overlays(currOverlays) {
          sorted_overlays = currOverlays.sort(compare);
          currOverlays.forEach(element =&gt; {
              console.log(element);
          });
  
      return sort_overlays;        
  }
  </code></pre>
  
  <p>However, it does not seem to work, since I was inspecting the final array (with 700+ points) and some look out of order.</p>
  
  <p>Am I applying the <code>compare</code> function correctly?</p>
  
  <p>kind regards</p>