-7

我有一个 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”进行数字排序?我在网上看到了很多东西,但我不明白它们:(

我真的很感谢答案!

谢谢!!

4

4 回答 4

0

First of all you're only adding one item to your list

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

after fixing that to something like google or there are several good answer here c# sort List. I really think you need to take a minute to learn a little more of c# basics , right now you have it written so you have to hard code every node of the xml in your c# , whats the point of even reading the XML file?

于 2013-06-22T09:39:52.300 回答
0

You can in-place sort scores if you want, using List.Sort(Comparison<T>):

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

Note that because Punten appears to be a string, you need to convert it to an int to compare properly.

If you change your code to make Punten an int, the sorting code would simplify to:

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

What's happening here is that we are supplying a comparison function with which the sort will compare pairs of items to determine the required order.

(a, b) in the code above specifies two parameters of the element type of the list to compare. We just need to compare their Punten properties.

于 2013-06-22T09:40:19.983 回答
0

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

 var sortedByPunten = scores.OrderByDescending(s => Int32.Parse(s.punten));
于 2013-06-22T09:31:43.433 回答
0

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

var scores = scores.OrderByDescending(x => Convert.ToInt32(x.Punten));
于 2013-06-22T09:34:22.170 回答