title: Sort latitude and longitude coordinates into clockwise ordered quadrilateral
body: <p><strong>Problem</strong></p>
  
  <p>Users can provide up to four latitude and longitude coordinates, in any order. They do so with Google Maps. Using Google's <code>Polygon</code> API (v3), the coordinates they select should highlight the selected area between the four coordinates.</p>
  
  <p><strong>Question</strong></p>
  
  <p>How do you sort an array of latitude and longitude coordinates in (counter-)clockwise order?</p>
  
  <p><strong>Solutions and Searches</strong></p>
  
  <p><em>StackOverflow Questions</em></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2353409/drawing-resizable-not-intersecting-polygons">Drawing resizable (not intersecting) polygons</a></li>
  <li><a href="https://stackoverflow.com/questions/2374708/how-to-sort-points-in-a-google-maps-polygon-so-that-lines-do-not-cross">How to sort points in a Google maps polygon so that lines do not cross?</a></li>
  <li><a href="https://stackoverflow.com/questions/242404/sort-four-points-in-clockwise-order">Sort Four Points in Clockwise Order</a></li>
  </ul>
  
  <p><em>Related Sites</em></p>
  
  <ul>
  <li><a href="http://www.daftlogic.com/projects-google-maps-area-calculator-tool.htm" rel="noreferrer">http://www.daftlogic.com/projects-google-maps-area-calculator-tool.htm</a></li>
  <li><a href="http://en.literateprograms.org/Quickhull_%28Javascript%29" rel="noreferrer">http://en.literateprograms.org/Quickhull_%28Javascript%29</a></li>
  <li><a href="http://www.geocodezip.com/map-markers_ConvexHull_Polygon.asp" rel="noreferrer">http://www.geocodezip.com/map-markers_ConvexHull_Polygon.asp</a></li>
  <li><a href="http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm" rel="noreferrer">http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm</a></li>
  </ul>
  
  <p><em>Known Algorithms</em></p>
  
  <ul>
  <li>Graham's scan (too complicated)</li>
  <li>Jarvis March algorithm (handles N points)</li>
  <li>Recursive Convex Hull (removes a point)</li>
  </ul>
  
  <p><strong>Code</strong></p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>// Ensures the markers are sorted: NW, NE, SE, SW
  function sortMarkers() {
    var ns = markers.slice( 0 );
    var ew = markers.slice( 0 );
  
    ew.sort( function( a, b ) {
      if( a.position.lat() &lt; b.position.lat() ) {
        return -1;
      }
      else if( a.position.lat() &gt; b.position.lat() ) {
        return 1;
      }
  
      return 0;
    });
  
    ns.sort( function( a, b ) {
      if( a.position.lng() &lt; b.position.lng() ) {
        return -1;
      }
      else if( a.position.lng() &gt; b.position.lng() ) {
        return 1;
      }
  
      return 0;
    });
  
    var nw;
    var ne;
    var se;
    var sw;
  
    if( ew.indexOf( ns[0] ) &gt; 1 ) {
      nw = ns[0];
    }
    else {
      ne = ns[0];
    }
  
    if( ew.indexOf( ns[1] ) &gt; 1 ) {
      nw = ns[1];
    }
    else {
      ne = ns[1];
    }
  
    if( ew.indexOf( ns[2] ) &gt; 1 ) {
      sw = ns[2];
    }
    else {
      se = ns[2];
    }
  
    if( ew.indexOf( ns[3] ) &gt; 1 ) {
      sw = ns[3];
    }
    else {
      se = ns[3];
    }
  
    markers[0] = nw;
    markers[1] = ne;
    markers[2] = se;
    markers[3] = sw;
  }
  </code></pre>
  
  <p>Thank you.</p>