title: How can I sort a coordinate list for a rectangle counterclockwise?
body: <p>I need to sort a coordinate list for a rectangle counterclockwise, and make the north-east corner the first coordinate. These are geographic coordinates (i.e. Longitude, Latitude) in decimal form.<sup>1</sup></p>
  
  <p>For example, here are the 4 corners of a rectangle, starting with the north-west corner and moving clockwise:</p>
  
  <pre><code>[
    { "lat": 34.495239, "lng": -118.127747 }, # north-west
    { "lat": 34.495239, "lng": -117.147217 }, # north-east
    { "lat": 34.095174, "lng": -117.147217 }, # south-east
    { "lat": 34.095174, "lng": -118.127747 }  # south-west
  ]
  </code></pre>
  
  <p>I need to sort these counterclockwise and change the "anchor"/starting point to be north-east:</p>
  
  <pre><code>[
    { "lat": 34.495239, "lng": -117.147217 }, # north-east
    { "lat": 34.495239, "lng": -118.127747 }, # north-west
    { "lat": 34.095174, "lng": -118.127747 }, # south-west
    { "lat": 34.095174, "lng": -117.147217 }  # south-east
  ]
  </code></pre>
  
  <p>I do not know what order the list will be in initially (i.e. clockwise or counterclockwise). I do not know which corner the first coordinate in the list represents.</p>
  
  <hr>
  
  <p><sup>1</sup><sub>This is not a true rectangle when mapped to the surface of the earth, however since I do have 2 opposing corners I am calling it a rectangle for readability. Shapes that wrap +180/-180 longitude or +90/-90 latitude are not an issue.</sub></p>