I am writing a program that uses the haversine formula to determine the x nearest locations to a lat/lon. It takes a base lat and lon and compares it to the lat and lon of every location element in an xml file and then it adds it to a custom List(Result) containing the site lat, site lon and haversine result (which is a decimal type and containes the distance between the base and the xml entry). I have it all working fine with no errors but I thought that there were closer locations than some of the top 10 displayed after sorting in the console so I moved it to wpf and got it to plot the top 10 onto a map (the Bing Maps Control for WPF) and also looked at the xml file. I found quite a few locations that were much, much, much closer to the base than the top 10 were so I then got it to add each haversine result to a new List(string) and print a huge messagebox with 1 result on each line and I looked through and saw that they were sorted, but incorrectly:
It should be (this is a very simplified example, the real would be decimals like 109.84477432975):
10, 50, 100, 150, 200, 500
But instead it is:
10, 100, 150, 200, 50, 500
It sorts them into groups of first digits and then sorts those groups by value. I get this in file explorer when I have consecutively named files. They only sort in the correct order if I put a zero at the start of the ones with single digits. How can I stop this and make the DECIMALS order correctly? I cannot change the haversine results once they are calculated and I am sorting my custom type list:
public class Result
{
public string SiteID { get; set; }
public string SiteName { get; set; }
public decimal SiteLat { get; set; }
public decimal SiteLon { get; set; }
public string HavResult { get; set; }
}
...by using this:
results.Sort(delegate(Result result1, Result result2) { return result1.HavResult.CompareTo(result2.HavResult); });
And I am sorting the list by only the HavResult part. How can it be sorted correctly, thank you.