我有这个变量:
List<Points> pointsOfList;
它包含未排序的点((x,y) - 坐标);
我的问题如何按X降序对列表中的点进行排序。
例如:
我有:(9,3)(4,2)(1,1)
我想得到这个结果:(1,1)(4,2)(9,3)
提前谢谢。
答案 0 :(得分:8)
pointsOfList.OrderBy(p=>p.x).ThenBy(p=>p.y)
答案 1 :(得分:6)
LINQ:
pointsOfList = pointsOfList.OrderByDescending(p => p.X).ToList();
答案 2 :(得分:1)
这个简单的控制台程序可以做到:
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);
}
}