【问题标题】:C# multiple variable leaderboardC#多变量排行榜
【发布时间】:2021-12-15 02:00:10
【问题描述】:

我正在用 C# 编写一个脚本,它需要一个函数来使用多个参数创建排行榜。 以下是排行榜之一的示例:

粗体列是排行榜的第一个检查项,然后移动到左侧的列,依此类推。

我的想法是为每一列创建一个 for 循环并对四个值进行排序。

int[] tempScores = new int[scores[0].Length];
for (int i = 0; i < scores[0].Length; i++)
{
  if (scores[0][i] < scores[0][i+1]) tempScores[i] = scores[0][i+1];
  else if (scores[0][i] > scores[0][i+1]) tempScores[i] = scores[0][i];
  else if (scores[0][i] == scores[0][i+1])
  {
    // Do same thing but for next column : scores[1]
  }
}

此代码可以按 90% 的时间运行,但有时会反转两个或多个值。而且它不适用于这样的排行榜:

因此,如果有人对我的问题有答案或更有效的方法,我很想知道如何。

【问题讨论】:

    标签: c# function sorting leaderboard


    【解决方案1】:

    我读到这里,首先想到的是 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"));
            }
    

    你去吧,测试通过了,你可以根据自己的内心进行重构,因为你知道你不会破坏实际的逻辑。

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多