Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

c# - Sorting two separate Lists in the same order?

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));
    }
  }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...