1.先写一个继承类IComparer,重载比较函数Compare,写自定义的比较方法。例:
public class CPointComparer : IComparer<Point>
{
public int Compare(Point p1, Point p2)
{
if (p1.X > p2.X)
return 1;
else if (p1.X < p2.X)
return -1;
else if (p1.Y > p2.Y)
return 1;
else if (p1.Y < p2.Y)
return -1;
else return 0;
}
}
2.以该方法为基础比较,例:
Point [] pt = new Point [4];
pt[0] = new Point(5, 1);
pt[1] = new Point(2, 2);
pt[2] = new Point(5, 2);
pt[3] = new Point(7, 2);
List<Point> lp = new List<Point>();
for (int i = 0; i < 4;i++ )
{
lp.Add(pt[i]);
}
lp.Sort(new CPointComparer());
C# 排序 List.Sort
最新推荐文章于 2023-05-10 14:36:12 发布