Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对列表进行排序<;点>;_C#_.net_Linq_List - Fatal编程技术网

C# 如何对列表进行排序<;点>;

C# 如何对列表进行排序<;点>;,c#,.net,linq,list,C#,.net,Linq,List,我有一个变量: List<Points> pointsOfList; 列表点列表; 它包含未排序的点((x,y)-坐标) 我的问题是如何按X降序排列列表中的点 例如: 我有这个:(9,3)(4,2)(1,1) 我想得到这个结果:(1,1)(4,2)(9,3) 提前谢谢你。林克: pointsOfList = pointsOfList.OrderByDescending(p => p.X).ToList(); 这个简单的控制台程序执行以下操作: class Program

我有一个变量:

List<Points> pointsOfList;
列表点列表;
它包含未排序的点((x,y)-坐标)

我的问题是如何按X降序排列列表中的点

例如:

我有这个:(9,3)(4,2)(1,1)

我想得到这个结果:(1,1)(4,2)(9,3)

提前谢谢你。

林克:

pointsOfList = pointsOfList.OrderByDescending(p => p.X).ToList();

这个简单的控制台程序执行以下操作:

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);
    }
}
类程序
{
静态void Main(字符串[]参数)
{    
列表点SOFLIST=新列表(){
新点(){x=9,y=3},
新点(){x=4,y=2},
新点(){x=1,y=1}
};
foreach(pointsOfList.OrderBy(p=>p.x)中的var点)
{
Console.WriteLine(points.ToString());
}
Console.ReadKey();
}
}
班级积分
{
公共整数x{get;set;}
公共整数y{get;set;}
公共重写字符串ToString()
{
返回string.Format(({0},{1})”,x,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);
    }
}