如何分类列表[复制]

时间:2022-05-31 15:58:29

This question already has an answer here:

这个问题已经有了答案:

I have this variable:

我有这个变量:

List<Points> pointsOfList;

it's contain unsorted points( (x,y) - coordinates);

它包含未排序的点((x,y) -坐标);

My question how can I sort the points in list by X descending.

我的问题是,我怎样才能将列表中的点按X向下排序。

for example:

例如:

I have this: (9,3)(4,2)(1,1)

3)我有一:(9日(4,2)(1)

I want to get this result: (1,1)(4,2)(9,3)

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

Thank you in advance.

提前谢谢你。

3 个解决方案

#1


6  

LINQ:

LINQ:

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

#2


7  

pointsOfList.OrderBy(p=>p.x).ThenBy(p=>p.y)

#3


1  

This simple Console program does that:

这个简单的控制台程序做到了:

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);
    }
}

#1


6  

LINQ:

LINQ:

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

#2


7  

pointsOfList.OrderBy(p=>p.x).ThenBy(p=>p.y)

#3


1  

This simple Console program does that:

这个简单的控制台程序做到了:

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);
    }
}