将排行榜从最高到最低排序

Sorting a leaderboard from highest to lowest

我正在尝试获取 richTextBox 中从最高到最低排序的测验分数。我将玩家姓名、测验和分数保存在名为 scoresClass 的 class 中。在测验结束时,我将 class 呼叫给三个 richTextBox,以显示他们的姓名、测验和分数。然后我将它们添加到列表并将列表写入文件。排行榜是 richTextBox,设置为等于文件中的数据。

这是我的代码:

public frmFinal()
{
    InitializeComponent();            
}

private void frmFinal_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.Exit();
}

List<string> scores = new List<string>();

private void frmFinal_Load(object sender, EventArgs e)
{
    //sets the leaderboard equal to the file scoreInfo
    rchBxScores.Text = File.ReadAllText(".\scoreInfo.bin");
    //sets a textbox equal to the players score
    rchBxScore.Text = Convert.ToString(scoresClass.score);
    rchBxNameScore.Text = scoresClass.name;
    rchBxQuizNameScore.Text = scoresClass.quiz;
}

private void btnClearScores_Click(object sender, EventArgs e)
{
    //opens the file scoreInfo
    FileStream fileStream = File.Open(".\scoreInfo.bin", FileMode.Open);
    //empties the file
    fileStream.SetLength(0);
    //closes the file
    fileStream.Close();
    //sets the leaderbaord equal to the file
    rchBxScores.Text = File.ReadAllText(".\scoreInfo.bin");
    scores.Clear();
}

//creates a bool variable and sets it equal to false
bool saved = false;

private void btnSaveScore_Click(object sender, EventArgs e)
{
    //checks if saved equals false
    if (saved == false)
    {
        //if saved equals false, it opens the file scoreInfo
        using (StreamWriter scoreInfo = new StreamWriter(".\scoreInfo.bin", true))
        {
            scores.Add(scoresClass.name + "\t" + scoresClass.quiz + "\t" + scoresClass.score);

            foreach(string score in scores)
            {                        
                scoreInfo.WriteLine(score);                        
            }                    
        }

        //clears all the players score details
        rchBxNameScore.Clear();
        rchBxQuizNameScore.Clear();
        rchBxScore.Clear();
        rchBxScores.Text = File.ReadAllText(".\scoreInfo.bin");
        //sets saved to true
        saved = true;
    }            
}

目前分数是按输入时间计算的,而不是按分数计算的。我不确定我会如何订购它们。

这里是 class:

public class scoresClass
{
    public static int score = 0;
    public static string name = "";
    public static string quiz = "";

    public scoresClass(string userName, int userScore, string userQuiz)
    {
        name = userName;
        score = userScore;
        quiz = userQuiz;
    }
}

由于您使用 StreamWriter 附加到文件,我会将文件作为 scoreClass 的集合读回,而不是盲目读取文件并将其转储到 richTextBox。沿着这些路线。

我也必须更改您的 class 定义。

public class scoresClass
{
    public int score = 0;
    public string name = "";
    public string quiz = "";

    public scoresClass(string userName, string userQuiz, int userScore)
    {
        name = userName;
        score = userScore;
        quiz = userQuiz;
    }
}

private List<scoresClass> importScoresFromFile(string path)
{
    var listOfScores = new List<scoresClass>();

    var rawScores = File.ReadAllLines(path);

    foreach (var score in rawScores)
    {
        string[] info = score.Split('\t');

        listOfScores.Add(new scoresClass(info[0], info[1], Convert.ToInt32(info[2])));
    }

    return listOfScores.OrderByDescending(r => r.score).ToList();
}

一旦您将所有分数记入内存,您就可以做一些 LINQ 工作来对它们进行排序。然后,您可以遍历 List<scoresClass> 中的每个值并根据需要导出到 richTextBox