0

我有一个带有 2 个(双)数组成员的构造函数:

constructor[i].x 和 constructor[i].y (其中 i 是元素的数量)

如何对 x 成员进行排序: constructor[].x ?

4

1 回答 1

1

使用 LINQ 它只是

constructor = constructor.OrderBy(a => a.x).ToArray();

没有 LINQ

class CustomClass
{
    public double x;
    public double y;
}

public class CustomComparer : IComparer<CustomClass>
{
    private CustomComparer() { }

    public static CustomComparer Instance { get { return _SingeltonInstance; } }

    private static CustomComparer _SingeltonInstance = new CustomComparer();

    public int Compare(CustomClass a, CustomClass b)
    {
        return a.x.CompareTo(b.x);
    }
}

public class myCode
{
    public void SomeFuction(CustomClass[] myClass)
    {
         //myClass is unsorted here;
         Array.Sort(myClass, CustomComparer.Instance);
         //myClass is sorted here;
    }
}
于 2012-04-22T18:09:36.817 回答