title: Sort a list of results by distance
body: <p>I have a web site the I have created to sell items locally which if free for everyone to use. But I have now come accross a little problem! Now people form further afield are joining I want them to be able to search and lit all items by distance from them and/or see all items in their area.</p>
  
  <p>I have been collecting everyones postcode and converting them to Easting and Northing coords not Latitude-longitude.</p>
  
  <p>Is the best way to create a new recodset on the fly do the distance calc then sort by distance or is there a better way that will use less resources on the site?</p>
  
  <p><strong><em>Here is the answer</em></strong></p>
  
  <pre><code>strSQL = "SELECT *, ( 3959 * acos( cos( radians( " &amp; strMyLat &amp; 
      ") ) * cos( radians(Latitude) ) * cos( radians(Longitude) - radians(" &amp; strMyLng &amp; 
      ") ) + sin( radians( " &amp; strMyLat &amp; 
      ") ) * sin( radians(Latitude) ) ) ) AS distance FROM TABLE WHERE HAVING distance &lt; 3;" '## 3 Miles
  </code></pre>