如何排序List
本文关键字:List Point 排序 何排序 | 更新日期: 2023-09-27 18:14:48
我有这样一个变量:
List<Points> pointsOfList;
包含未排序的点((x,y) -坐标);
我的问题是如何按X降序排序列表中的点
例如:有:(9,3)(4,2)(1,1)
我想得到这样的结果:(1,1)(4,2)(9,3)
提前感谢。
LINQ:
pointsOfList = pointsOfList.OrderByDescending(p => p.X).ToList();
pointsOfList.OrderBy(p=>p.x).ThenBy(p=>p.y)
这个简单的控制台程序就是这样做的:
class Program
{
static void Main(string[] args)
{
List<Points> pointsOfList = new List<Points>(){
new Points() { x = 9, y = 3},
new Points() { x = 4, y = 2},
new Points() { x = 1, y = 1}
};
foreach (var points in pointsOfList.OrderBy(p => p.x))
{
Console.WriteLine(points.ToString());
}
Console.ReadKey();
}
}
class Points
{
public int x { get; set; }
public int y { get; set; }
public override string ToString()
{
return string.Format("({0}, {1})", x, y);
}
}