简体   繁体   中英

Sorting a leaderboard from highest to lowest

I'm trying to get the scores of a quiz ordered from highest to lowest in a richTextBox . I save the players name, quiz and score in a class called scoresClass . At the end of the quiz, I call the class to three richTextBox s, to show their name, quiz and score. I then add them to a list and write the list to a file. The leaderboard, which is a richTextBox , is set equal to the data in the file.

Here is my code:

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;
    }            
}

Currently the scores are going by the time they were entered not by score. I'm not sure how i would actually order them.

Here is the 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;
    }
}

Since you're appending to a file using the StreamWriter I would read the file back in as a collection of scoreClass instead of just blind reading the file and dumping it into a richTextBox . Something along these lines.

I had to change your class definition as well.

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();
}

Once you have all the scores in memory you can then do a little LINQ work to sort them. You can then iterate through each value in the List<scoresClass> and export to the richTextBox 's as desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM