C# 将Highscorelist从文本文件读入按分数排序的文本框

C# 将Highscorelist从文本文件读入按分数排序的文本框,c#,winforms,list,text-files,C#,Winforms,List,Text Files,我已经编写了一个游戏。在游戏中,玩家收集点数,下一步玩家可以将自己的名字写在文本框中,点击“发送”按钮,他的名字和点数将保存在一个.txt文件中。 玩家可以在文本框中读取自己或其他玩家姓名的最高分,该文本框将读取保存的文本文件的名称和分数。 文本文件如下所示: [Name] [Points] Name1 60 Name2 20 Name3 90 Name4 80 如您所见,我的问题是,文本文件没有按分数排序。我如何编程文本框或具有最高数

我已经编写了一个游戏。在游戏中,玩家收集点数,下一步玩家可以将自己的名字写在文本框中,点击“发送”按钮,他的名字和点数将保存在一个.txt文件中。

玩家可以在文本框中读取自己或其他玩家姓名的最高分,该文本框将读取保存的文本文件的名称和分数。 文本文件如下所示:

[Name]     [Points]
 Name1      60
 Name2      20
 Name3      90
 Name4      80
如您所见,我的问题是,文本文件没有按分数排序。我如何编程
文本框
或具有最高数字的播放器为第一、第二等的文本文件

当前代码:

using System.IO;
private void button1_Click(object sender, EventArgs e)
{
        s_Textfile = "Highscore.txt";
        StreamWriter o_StreamW;
        if (!File.Exists(s_Textfile))
        {
            o_StreamW = File.CreateText(s_Textfile);
            o_StreamW.Close();
        }
        string s_Spacebar = "";
        s_Name = PlayersName.Text; //The Name the player wrotes in the textBox
        s_Highscore = Points.Text; //The Points the Player has

        o_StreamW.WriteLine(s_Name + s_Spacebar+ s_Highscore);
        o_StreamW.Close();
 }
下面是我的Highscore
文本框当前的样子:

StreamReader o_StreamR;
string s_Line = "";
string s_AllLines = "";

o_StreamR = new StreamReader(GameOverScreen.s_Textfile);
while (o_StreamR.Peek() != -1)
{
    s_Line= o_StreamR.ReadLine();
    s_AllLines = s_AllLines + s_Line + "\r\n";
}
o_StreamR.Close();
HighscoreRichTextBox.Text += "Name:             Score:" + "\r\n";
HighscoreRichTextBox.Text += s_AllLines;

我会首先使用一个标签作为你的球员名字和分数的分隔符,因为它更容易解析。创建一个包含分数和名称的结构,然后读入文件内容,创建列表,排序和输出

    //struct to hold the values
    struct highScoreEntry
    {
        public string PlayerName;
        public string Score;
        public highScoreEntry(string playerName, string playerScore)
        {
            PlayerName = playerName;
            Score = playerScore;
        }
    }



    List<highScoreEntry> allScores = new List<highScoreEntry>();
    using (System.IO.StreamReader sr = new StreamReader(@"Path to file"))
    {
        //skip the first line name and score titles
        string line = sr.ReadLine();
        while (!sr.EndOfStream)
        {
            line = sr.ReadLine();
            string[] lineParts = line.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
            allScores.Add(new highScoreEntry(lineParts[0], lineParts[1]));
        }

    }
    'sort in descending order using OrderByDescending
    List<highScoreEntry> sortedList = allScores.OrderByDescending(i => i.Score).ToList<highScoreEntry>();
    string highScoreText = "Player\tScore\r\n";
    foreach (highScoreEntry item in sortedList)
    {
        ''can be written directly into the textbox but for the example write it to a string
        highScoreText += item.PlayerName + "\t" + item.Score + "\r\n";
    }
    //add it to the textbox
    HighscoreRichTextBox.Text = highScoreText;

    }
//用于保存值的结构
结构highScoreEntry
{
公共字符串播放器名称;
公共弦乐谱;
公开高分条目(字符串播放器名称、字符串播放器核心)
{
PlayerName=PlayerName;
得分=球员核心得分;
}
}
List allScores=新列表();
使用(System.IO.StreamReader sr=new StreamReader(@“文件路径”))
{
//跳过第一行名称和分数标题
字符串行=sr.ReadLine();
而(!sr.EndOfStream)
{
line=sr.ReadLine();
string[]lineParts=line.Split(新字符串[]{“\t”},StringSplitOptions.RemoveEmptyEntries);
添加(新的HighScore条目(lineParts[0],lineParts[1]);
}
}
'使用OrderByDescending按降序排序
List sortedList=allScores.OrderByDescending(i=>i.scores.ToList();
字符串highScoreText=“Player\tScore\r\n”;
foreach(分类列表中的高分条目项)
{
''可以直接写入文本框,但在本例中,将其写入字符串
highScoreText+=item.PlayerName+“\t”+item.Score+“\r\n”;
}
//将其添加到文本框中
HighscoreRichTextBox.Text=highScoreText;
}

你所有的帖子得分都在0分或以下。现在是阅读的好时机,在网站限制您的帐户之前,阅读,或者访问,以了解网站如何工作,如何提出好的问题等。那里似乎没有任何代码可以对任何内容进行排序,因此还没有任何帮助