如何在C#中对List进行排序[重复]

时间:2021-04-04 15:59:02

This question already has an answer here:

这个问题在这里已有答案:

I have a class:

我上课了:

public class MyObject
{
public string Name;
public int Age;
}

I have a List of Myobject objects:

我有一个Myobject对象列表:

Name Age
ABC 12
BBC 14
ABC 11

How to sort this list with condition: sort Name first & sort Age later. With this list, the result after sorting:

如何使用条件对此列表进行排序:首先排序名称,然后排序年龄。使用此列表,排序后的结果:

Name Age
ABC 11
ABC 12
BBC 14

5 个解决方案

#1


10  

class Program
{
    static void Main(string[] args)
    {
        var list = new List<MyObject>(new[]
        {
            new MyObject { Name = "ABC", Age = 12 },
            new MyObject { Name = "BBC", Age = 14 },
            new MyObject { Name = "ABC", Age = 11 },
        });
        var sortedList = from element in list
                         orderby element.Name
                         orderby element.Age
                         select element;

        foreach (var item in sortedList)
        {
            Console.WriteLine("{0} {1}", item.Name, item.Age);
        }
    }
}

#2


14  

Two different ways using LINQ:

使用LINQ的两种不同方式:

1) Using OrderBy and ThenBy:

1)使用OrderBy和ThenBy:

l = l.OrderBy(x => x.Name).ThenBy(x => x.Age).ToList();

2) Using the query syntax:

2)使用查询语法:

l = (from x in l
     orderby x.Name, x.Age
     select x).ToList();

#3


3  

Using System.Linq you can acheive it easily:

使用System.Linq,您可以轻松实现:

list = list.OrderBy(e=>e.Name).ThenBy(e=>e.Age);

Also check this answer: Sorting a list using Lambda/Linq to objects.

另请检查以下答案:使用Lambda / Linq对列表对对象进行排序。

#4


1  

You can do the following using LINQ:

您可以使用LINQ执行以下操作:

class Program
{
    static void Main(string[] args)
    {
        List<MyObject> list = new List<MyObject>();

        list.Add(new MyObject() { Age = 12, Name = "ABC" });
        list.Add(new MyObject() { Age = 11, Name = "ABC" });
        list.Add(new MyObject() { Age = 14, Name = "BBC" });

        var sorted = list.OrderBy(mo => mo.Name).ThenBy(mo => mo.Age);

        foreach (var myObject in sorted)
        {
            Console.WriteLine(string.Format("{0} - {1}",
                              myObject.Name, myObject.Age));
        }
    }
}

#5


0  

You can pass an new Object to Order By so that it Orders By that

您可以将新对象传递给Order By,以便按顺序排序

class Program
{
    static void Main(string[] args)
    {
        var list = new List<MyObject>(new[]
        {
            new MyObject { Name = "ABC", Age = 12 },
            new MyObject { Name = "BBC", Age = 14 },
            new MyObject { Name = "ABC", Age = 11 },
        });
        var sortedList = list.OrderBy( x => new { x.Name , x.Age });

        foreach (var item in sortedList)
        {
            Console.WriteLine("{0} {1}", item.Name, item.Age);
        }
    }
}

#1


10  

class Program
{
    static void Main(string[] args)
    {
        var list = new List<MyObject>(new[]
        {
            new MyObject { Name = "ABC", Age = 12 },
            new MyObject { Name = "BBC", Age = 14 },
            new MyObject { Name = "ABC", Age = 11 },
        });
        var sortedList = from element in list
                         orderby element.Name
                         orderby element.Age
                         select element;

        foreach (var item in sortedList)
        {
            Console.WriteLine("{0} {1}", item.Name, item.Age);
        }
    }
}

#2


14  

Two different ways using LINQ:

使用LINQ的两种不同方式:

1) Using OrderBy and ThenBy:

1)使用OrderBy和ThenBy:

l = l.OrderBy(x => x.Name).ThenBy(x => x.Age).ToList();

2) Using the query syntax:

2)使用查询语法:

l = (from x in l
     orderby x.Name, x.Age
     select x).ToList();

#3


3  

Using System.Linq you can acheive it easily:

使用System.Linq,您可以轻松实现:

list = list.OrderBy(e=>e.Name).ThenBy(e=>e.Age);

Also check this answer: Sorting a list using Lambda/Linq to objects.

另请检查以下答案:使用Lambda / Linq对列表对对象进行排序。

#4


1  

You can do the following using LINQ:

您可以使用LINQ执行以下操作:

class Program
{
    static void Main(string[] args)
    {
        List<MyObject> list = new List<MyObject>();

        list.Add(new MyObject() { Age = 12, Name = "ABC" });
        list.Add(new MyObject() { Age = 11, Name = "ABC" });
        list.Add(new MyObject() { Age = 14, Name = "BBC" });

        var sorted = list.OrderBy(mo => mo.Name).ThenBy(mo => mo.Age);

        foreach (var myObject in sorted)
        {
            Console.WriteLine(string.Format("{0} - {1}",
                              myObject.Name, myObject.Age));
        }
    }
}

#5


0  

You can pass an new Object to Order By so that it Orders By that

您可以将新对象传递给Order By,以便按顺序排序

class Program
{
    static void Main(string[] args)
    {
        var list = new List<MyObject>(new[]
        {
            new MyObject { Name = "ABC", Age = 12 },
            new MyObject { Name = "BBC", Age = 14 },
            new MyObject { Name = "ABC", Age = 11 },
        });
        var sortedList = list.OrderBy( x => new { x.Name , x.Age });

        foreach (var item in sortedList)
        {
            Console.WriteLine("{0} {1}", item.Name, item.Age);
        }
    }
}