Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:46+00:00 2026-05-28T07:17:46+00:00

Just to lay down some restrictions first. Because of the environment I’m coding this

  • 0

Just to lay down some restrictions first. Because of the environment I’m coding this in, I cannot create my own classes or methods. Just basic procedural code. It’s in a CMS, and my code is executed inside a method itself.

Here’s my question

On this page, I do a database query, loading all 700ish store locations. I then do a distance calculation based on the lat and lng values in the query string against the ones in the database to find the stores within 50 kilometers. Each one that is within that distance, I currently add to a List<DataRow>. I also take the distance result, round it to one decimal place, and store that into a simple list of type Double. What I want to do, is basically sort these so when i output the stores + distance value, it is sorted shortest to longest distance. I was thinking of switching the List<DataRow> and List<Double> combo to a Dictionary<Double, DataRow> and use sorting there, but there is of course certain cases where two places have the same distance value, so therefore it would not be unique. Is there another collection type I can use for this, or can you recommend a good way to sort the data?

Here’s my code in case you need a visual:

The PRA object is basically the main object we use for working with the backend aspects of our CMS. In this case I’m using a short hand method to query the data, and check a couple variables in the request data. The rest is all built in .net stuff.

List<DataRow> locationsInRange = new List<DataRow>();
List<Double> distances = new List<Double>();
if(!String.IsNullOrEmpty(PRA.Request.QueryString["lat"]) && !String.IsNullOrEmpty(PRA.Request.QueryString["lng"])) {
  Double earthRadius = 6371.0;
  Double maxDistance = 50.0;

  var locations = PRA.QueryDataRows("*", "", "{Location}", "showweb");
  Double lat =  Double.Parse(PRA.Request.QueryString["lat"]);
  Double lng =  Double.Parse(PRA.Request.QueryString["lng"]);

  if(!String.IsNullOrEmpty(PRA.Request.QueryString["radius"])) {
    Double temp = Double.Parse(PRA.Request.QueryString["radius"]);
    if(temp > 0) {
      maxDistance = temp;
    }
  }

  bool firstError = true;
  foreach(var l in locations) {
    Double latSecond = 0.0;
    Double lngSecond = 0.0;
    try {
      latSecond = Double.Parse(l["lat"].ToString());
      lngSecond = Double.Parse(l["lng"].ToString());
    }
    catch(Exception ex) {
      // do nothing. The lat and lng may not of been valid or not returned a result
    }


    Double dLat = Math.PI * ((lat - latSecond) / 180.0);
    Double dLon = Math.PI * ((lng - lngSecond) / 180.0);

    Double lat1 = Math.PI * (latSecond / 180.0);
    Double lat2 = Math.PI * (lat / 180.0);

    Double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
    Double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    if(earthRadius * c <= (Double) maxDistance) {
      locationsInRange.Add(l);
      distances.Add(Math.Round(earthRadius * c, 1));
    }
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:17:46+00:00Added an answer on May 28, 2026 at 7:17 am

    Use a list of pairs (data, distance), represented by the Tuple type…

    var locations = new List<Tuple<DataRow, double>>();
    locations.Add(Tuple.Create(row, distance));
    locations.Sort((x, y) => x.Item2.CompareTo(y.Item2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just found this out, so i am answering my own question :) Use a
I'm just moving from svn to git, and am keen to lay some good
Just looking for the first step basic solution here that keeps the honest people
I guess its just due to my lake of jquery knowledge but ill lay
Update: Thanks for everyone who helped out - the answer to this one lay
OKay, I'm from a PHP background, but I've just been tasked with developing some
I need to lay a variable number of views (may just be one) next
I will do my best to lay this out in text. Essentially, we have
This should be a pretty straightforward classes and interfaces question, but please bear with
today i just lay on my bed meditating programming stuff when an idea flows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.