排序和合并区间c#

排序和合并区间c#,c#,sorting,C#,Sorting,我有一个2D空间中的线段间隔列表。我的段类定义如下: class Segment { public int startPoint { get; set; } public int endPoint { get; set; } public int commonPoint { get; set; } } class Segment { public int startPoint { get; set; } public int endPoint { get

我有一个2D空间中的线段间隔列表。我的段类定义如下:

class Segment
{
    public int startPoint { get; set; }
    public int endPoint { get; set; }
    public int commonPoint { get; set; }
}
class Segment
{
    public int startPoint { get; set; }
    public int endPoint { get; set; }
    public int commonPoint { get; set; }
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        Segment objAsSegment = obj as Segment;
        if (objAsSegment == null) return false;
        else return Equals(objAsSegment);
    }
    public int sortByStartingPointAscending(int startPoint1, int startPoint2)
    {
        return startPoint1.CompareTo(startPoint2);
    }
    public int CompareTo(Segment compareSegment)
    {
        if (compareSegment == null) return 1;
        else
            return this.startPoint.CompareTo(compareSegment.startPoint);
    }
    public override int GetHashCode()
    {
        return startPoint;
    }
    public bool Equals(Segment other)
    {
        if (other == null) return false;
        return (this.startPoint.Equals(other.startPoint));
    }
}
horizontalSegments.Sort(CompareSegmentHorizontal);
每个线段都有一个起点、终点和公共点,所以我知道哪些线段是共线的。在我的主程序中,我阅读了用户以这种格式输入的一些行:{startpoint.X,startpoint.Y,endPoint.X,endPoint.Y}
因此,用户输入如下数据:

4 //this is the number of lines  
0 1 2 1  
1 4 1 2  
0 3 2 3  
2 1 6 1  
下面是读取数据的代码:

static void Main(string[] args)
{
    int n = 0; //number of input segments
    n = Convert.ToInt32(Console.ReadLine());

    string[] values; //hold input segments' startpoints and endpoints
    int x1, y1, x2, y2;
    for (int i = 0; i < n; i++)
    {
        values = Console.ReadLine().Split(' ');
        x1 = Convert.ToInt32(values[0]); y1 = Convert.ToInt32(values[1]);
        x2 = Convert.ToInt32(values[2]); y2 = Convert.ToInt32(values[3]);

        if (x1 == x2) //this is a vertical line
        {
            if(y1 > y2) //storing the segment intervals form left to right
                verticalSegments.Add(new Segment { startPoint = y2, endPoint = y1, commonPoint = x1 });
            else
                verticalSegments.Add(new Segment { startPoint = y1, endPoint = y2, commonPoint = x1 });
        }
        else // this is a horizontal line
        {
            if (x1 > x2) //storing the segment intervals form left to right
                horizontalSegments.Add(new Segment { startPoint = x2, endPoint = x1, commonPoint = y1 });
            else
                horizontalSegments.Add(new Segment { startPoint = x1, endPoint = x2, commonPoint = y1 });
        }
    }
我调用了
horizontalSegments.Sort()在我的主方法中。但我有一个错误:


调用
sort()
行中未处理
InvalidOperationException

创建并实现一个方法,该方法对两个
对象进行所需的比较,并将其传递到列表的
排序
方法中

比较方法的签名需要如下(可以是静态的):

然后像这样使用它:

class Segment
{
    public int startPoint { get; set; }
    public int endPoint { get; set; }
    public int commonPoint { get; set; }
}
class Segment
{
    public int startPoint { get; set; }
    public int endPoint { get; set; }
    public int commonPoint { get; set; }
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        Segment objAsSegment = obj as Segment;
        if (objAsSegment == null) return false;
        else return Equals(objAsSegment);
    }
    public int sortByStartingPointAscending(int startPoint1, int startPoint2)
    {
        return startPoint1.CompareTo(startPoint2);
    }
    public int CompareTo(Segment compareSegment)
    {
        if (compareSegment == null) return 1;
        else
            return this.startPoint.CompareTo(compareSegment.startPoint);
    }
    public override int GetHashCode()
    {
        return startPoint;
    }
    public bool Equals(Segment other)
    {
        if (other == null) return false;
        return (this.startPoint.Equals(other.startPoint));
    }
}
horizontalSegments.Sort(CompareSegmentHorizontal);

次要提示:与Java不同,在C#中,用PascalCase表示法编写方法和属性名是很常见的。

假设您的
水平段
列表
,该方法要求T实现
IComparable
接口,而您忘记将其添加到
类的声明中。这是无效手术例外的原因
Segment
类已经有了
CompareTo
方法,该方法是
iCorable
接口所需的。那就加上它吧

class Segment : IComparable<Segment>
类段:i可比较

我没有看到任何问题…编辑:D忘记问:D
我该怎么做?
不是问题。这是“为我做这件事”的含蓄要求。解释你的代码,你为它尝试了什么,出了什么问题…然后我该怎么做?我不知道怎么做?我应该如何编辑我的问题?我对它投了更高的票…我如何将其传递给排序方法?