简体   繁体   中英

Sorting points/records by latitude and longitude (by distance -nearest to farthest)

I know this has been talked to death in some forums and I've found a couple different ways to do it. By way of example, this is a stackoverflow post discussing it and this is an msdn post talking about it. Both seem interesting yet seem to accomplish it for the original authors in different ways. They seem happy with the answers provided but I'm not getting satisfactory results (read: not consistently sorted correctly)

The question is twofold:

  1. Is it more efficient to sort these in C# post data retrieval or in SQL by calculating a distance from point of origin and using order by (or other method)? I could end up needing to sort 3000+ locations by distance from point of origin (as denoted by lat and long)

  2. What would be the most "correct" way to sort these points according to a lat/long of origin? By "correct" I don't by method but by actual distance from point of origin. Of course location of sorting comes into play because of question 1.

The technologies used are EF, SQL stored proc, C#.

Although there's probably a more efficient way to do this, reading the two posts carefully and through some trial and error....

  1. The formula for determining the distance as put forth in the two posts referenced in my questions are essentially the same.

  2. It is important to have the list in approximate order first so run the ordering or sorting according to the formula(s) given.

  3. After the first sort, sort it again but take into account the "fudge" as noted in Teasel's answer whether it be in sql or code (in my case, C#).

    • It's also important to note that this will always always always be only an approximation and never exact but you can get really close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM