是否可以使用合并排序（C＃）根据多个条件对数组进行排序？

``````A 10 20 30
B 5 10 40
C 40 10 20
D 30 25 10
``````

``````void Main()
{
var products = new List<Product>();
products.Add(new Product() { Name = "A", Price = 10, Score = 20, Endurance = 30 });
products.Add(new Product() { Name = "B", Price = 5, Score = 10, Endurance = 40 });
products.Add(new Product() { Name = "C", Price = 40, Score = 10, Endurance = 20 });
products.Add(new Product() { Name = "D", Price = 30, Score = 25, Endurance = 10 });

var resultsSorted = products
.OrderBy(p => p.Price)
.ThenBy(p => p.Score)
.ThenBy(p => p.Endurance)
.ThenBy(p => p.Name);
}

class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public int Score { get; set; }
public int Endurance { get; set; }
}

``````

[更新]

``````   foreach(var product in resultsSorted)
{
Console.WriteLine(\$"Name:{Name}, Price:{Price}, Score:{Score}, Endurance:{Endurance}");
}
``````

``````
class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
public int Score { get; set; }
public int Endurance { get; set; }
public override string ToString()
{
return \$"Name:{Name}, Price:{Price}, Score:{Score}, Endurance:{Endurance}";
}
}
``````

``````foreach (var product in resultsSorted)
{
Console.WriteLine(product);
}
``````

0 条评论