【问题标题】:Sort list numericallyC# [closed]按数字排序列表# [关闭]
【发布时间】:2013-06-22 17:40:19
【问题描述】:

我有一个 xml 文件:

<highscore>
  <score>
    <naam>rake</naam>
    <punten>100</punten>
  </score>
  <score>
    <naam>john</naam>
    <punten>200</punten>
</score>
</highscore>

并且代码将值放在列表中并显示:

public Highscores()
    {
        InitializeComponent();

        XmlNode node = this.xmlbeheer.Open("Highscores/Highscores.xml");
        List<Score> scores = new List<Score>();


        foreach (XmlNode score in node.ChildNodes)
        {

            if (score.Name == "score")
            {
                Score s = new Score();
                foreach (XmlNode child in score.ChildNodes)
                {
                    if (child.Name == "naam")
                    {
                       s.Naam = child.InnerText;
                    }
                    if (child.Name == "punten")
                    {
                        s.Punten = child.InnerText;
                    }                     
                }
                scores.Add(s);
            }
        }

        foreach (Score s in scores)
        {
            if (n < 5)
            {
                Label naam = new Label();
                naam.Top = 10 + 23 * n;
                naam.Text = (n + 1) + ". " + s.Naam;
                naam.Left = 0;
                pnlScores.Controls.Add(naam);

                Label punten = new Label();
                punten.Top = 10 + 23 * n;
                punten.Text = s.Punten;
                punten.Left = 140;
                pnlScores.Controls.Add(punten);
            }
            n++;
        }
    }

但我的问题是如何将“punten”从高到低进行数字排序? 我在网上看到了很多东西,但我不明白它们:(

我真的很感激答案!

谢谢!!

【问题讨论】:

标签: c# xml sorting


【解决方案1】:

如果需要,您可以使用List.Sort(Comparison&lt;T&gt;) 就地排序scores

scores.Sort((a, b) => int.Parse(a.Punten).CompareTo(int.Parse(b.Punten)));

请注意,由于Punten 似乎是一个字符串,您需要将其转换为int 才能正确比较。

如果您更改代码以使 Punten 成为 int,则排序代码将简化为:

scores.Sort((a, b) => a.Punten.CompareTo(b.Punten));

这里发生的事情是我们提供了一个比较函数,排序将使用该函数比较成对的项目以确定所需的顺序。

上面代码中的(a, b)指定了要比较的列表元素类型的两个参数。我们只需要比较它们的Punten 属性即可。

【讨论】:

    【解决方案2】:

    由于punten 是一个字符串,您必须将其转换为int,然后您可以使用OrderByDescending 并执行以下操作:

     var sortedByPunten = scores.OrderByDescending(s => Int32.Parse(s.punten));
    

    【讨论】:

    • s.Punten = child.InnerText;这一行表示它是一个字符串。
    • @MikePrecup - 好地方。他必须将其更改为 int 或执行强制转换。
    【解决方案3】:

    您应该使用OrderByDescending LINQ 方法并在该方法中将您的字符串转换为整数(例如使用Convert.ToInt32):

    var scores = scores.OrderByDescending(x => Convert.ToInt32(x.Punten));
    

    【讨论】:

      【解决方案4】:

      首先,您只需将一项添加到您的列表中

      Score s = new Score();
                      foreach (XmlNode child in score.ChildNodes)
                      {
                          if (child.Name == "naam")
                          {
                             s.Naam = child.InnerText;
                          }
                          if (child.Name == "punten")
                          {
                              s.Punten = child.InnerText;
                          } 
                       scores.Add(s); // <-- move here and delete the one under this
                      }
                      scores.Add(s); // <-- see adding s to the List s outside the loop
      

      在将其修复为谷歌之类的东西之后,或者这里有几个很好的答案 c# sort List。我真的认为你需要花一点时间来学习更多的 c# 基础知识,现在你已经编写好了它,所以你必须在你的 c# 中对 xml 的每个节点进行硬编码,甚至阅读 XML 文件有什么意义?

      【讨论】:

      • 我猜 OP 发布了他的文件样本,而不是全部。但是感谢您发布答案:)
      • 我明白了,但也许又不是 - OP 正在询问如何对通用列表进行排序,这让我相信 OP 对编程很新鲜
      猜你喜欢
      • 1970-01-01
      • 2019-05-16
      • 2013-10-22
      • 1970-01-01
      • 2013-07-14
      • 2021-12-20
      • 2020-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多