我读到这里,首先想到的是 Linq。您只需按您关心的第一个属性排序,然后按所有其他属性排序。
首先是创建一个可以保存您的国家/地区数据的类。
它可能看起来像这样:
public class CountryData
{
public string Name { get; set; }
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
public int Prop4 { get; set; }
public int Prop5 { get; set; }
public int Prop6 { get; set; }
public int Prop7 { get; set; }
public int Prop8 { get; set; }
}
我不知道这些属性是什么意思,因为你没有说,所以我现在给它们一个通用名称。您可以随时将其更改为更有意义的内容
好的,现在我们有了这个,我们需要实际的订购。
我们可以编写另一个类来做到这一点:
public class OrderLogic
{
public List<CountryData> SortCountries(List<CountryData> countriesData)
{
return countriesData
.OrderByDescending(p => p.Prop8)
.ThenByDescending(p => p.Prop7) //continue with as many orders as you need
.ToList();
}
}
最后,我们如何使用它?
我会为此编写一个测试,以确保逻辑运行良好:
[Test]
public void Test1()
{
List<CountryData> countriesData = new List<CountryData>();
countriesData.Add(new CountryData { Name = "Switzerland", Prop1 = 3, Prop2 = 1, Prop3 = 1, Prop4 = 1, Prop5 = 4, Prop6 = 5, Prop7 = -1, Prop8 = 4 });
countriesData.Add(new CountryData { Name = "Italy", Prop1 = 3, Prop2 = 3, Prop3 = 0, Prop4 = 0, Prop5 = 7, Prop6 = 0, Prop7 = 7, Prop8 = 9 });
countriesData.Add(new CountryData { Name = "Wales", Prop1 = 3, Prop2 = 1, Prop3 = 1, Prop4 = 1, Prop5 = 3, Prop6 = 2, Prop7 = 1, Prop8 = 4 });
countriesData.Add(new CountryData { Name = "Turkey", Prop1 = 3, Prop2 = 0, Prop3 = 0, Prop4 = 3, Prop5 = 1, Prop6 = 8, Prop7 = -7, Prop8 = 0 });
var result = new OrderLogic().SortCountries(countriesData);
Assert.IsTrue(result[0].Name.Equals("Italy"));
Assert.IsTrue(result[1].Name.Equals("Wales"));
Assert.IsTrue(result[2].Name.Equals("Switzerland"));
Assert.IsTrue(result[3].Name.Equals("Turkey"));
}
你去吧,测试通过了,你可以根据自己的内心进行重构,因为你知道你不会破坏实际的逻辑。