简体   繁体   English

二变量排序算法

[英]Two variable sorting algorithm

I'm looking for an algorthm for sorting location points (latitude and longitude) from west to east and south to north. 我正在寻找一种算法,用于对从西到东以及从南到北的位置点(纬度和经度)进行排序。

When sorting, the points should be sorted starting in the west and south. 排序时,应从西部和南部开始对点进行排序。 When two points are compared, the longitude is compared first. 比较两个点时,首先比较经度。 The greater value (more west) point is higher in the list. 值越大(越靠西)的点在列表中越高。 If the two points have the same longitude, unlikely but possible, the latitudes of the two points are compared. 如果两个点的经度相同(不太可能但可能),则将比较两个点的纬度。 The lowest value (more south) is placed higher in the list. 最低值(越南)在列表中越高。

Does this algorthm exist somewhere? 这个算法存在于某个地方吗? Maybe in C#? 也许在C#中?

ps- these calculations will be limited to points within the continental United States. ps-这些计算将仅限于美国大陆范围内的点。 There will be no negative latitude / longitude values. 不会出现负的纬度/经度值。

using System.Linq;

var sortedPoints = points.OrderByDescending(p => p.Longitude).ThenBy(p => p.Latitude);

The algorithm doesn't exist out-of-the box in .NET (C# is a language and doesn't generally implement algorithms, you will typically find it in the .NET Base Class Library). 该算法在.NET中不是开箱即用的(C#是一种语言,通常不实现算法,通常会在.NET基类库中找到它)。

However, you could easily create a Coordinates structure/class with Latitude / Longitude properties (each as a double , I take it) and then implement IComparable<T> . 但是,您可以轻松创建具有Latitude / Longitude属性的Coordinates结构/类(每个都为double ),然后实现IComparable<T>

The implementation would then look something like this: 然后,实现将如下所示:

public class Coordinates : IComparable<Coordinates>
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public int CompareTo(Coordinates other)
    {
        // If the other instance is null, assume that
        // it is at 0,0?  You need to make that determination.
        if (other == null) return 1;

        // Compare longitude (double implements
        // IComparable<double>.
        int comparison = Longitude.CompareTo(other.Longitude);

        // If not 0, return the value.
        if (comparison <> 0) return comparison;

        // Compare latitude.  Inverse the result, as the more
        // south point (closer to 0) is greater.
        // Just return the value, if they are different, the
        // comparison value will be correct, if they are the
        // same, then comparison will be 0.
        return -Latitude.CompareTo(other.Latitude);
    }
}

Now, you can populate instances of these, place them in an array and pass it to the static Sort method on the Array class . 现在,您可以填充这些实例,将它们放置在数组中,并将其传递给Array静态Sort方法 The Sort method will use the IComparable<T> implementation to sort the array. Sort方法将使用IComparable<T>实现对数组进行排序。

Or you can place them in a List<T> (probably easier, since you might not know the number of elements beforehand) and then call the Sort method on the instance; 或者,您可以将它们放在List<T> (可能更容易,因为您可能事先不知道元素的数量),然后在实例上调用Sort方法 it too will use the IComparable<T> implementation to sort itself. 它也将使用IComparable<T>实现对其进行排序。

You also mentioned two points being the same. 您还提到了两点相同。 Since Latitude and Longitude are represented as a double , you run the risk of floating point errors. 由于LatitudeLongitude表示为double ,因此存在浮点错误的风险。 If you want to mitigate these errors, you can easily change the property to a decimal (which guarantees precision, up to a certain point); 如果要减轻这些错误,则可以轻松地将该属性更改为decimal (保证精度,直到特定点); this way, you will guarantee precision, and it implements IComparable<decimal> , which means that the implementation of IComparable<Coordinates> will just work with the switch. 这样,您将保证精度,并且它实现了IComparable<decimal> ,这意味着IComparable<Coordinates>的实现将仅与该开关一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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