-2

I'm trying to put the scores of my leaderboard in order of highest to lowest. I'm not sure how i would do this, i know this is not a place to ask for code to be written but I've looked everywhere, would I need to create an array or list, or a class?

namespace Coursework___Quiz
{
    public partial class frmFinal : Form
    {
        public frmFinal()
        {
            InitializeComponent();
        }

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

        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);
            //reads the first line of the file InputInfo and sets a string equal to it
            string nameScore = File.ReadAllLines(".\\InputInfo.bin").Skip(0).Take(1).First();
            //sets a textbox equal to the string nameScore
            rchBxNameScore.Text = nameScore;
            //reads the second line of the file InputInfo and sets a string equal to it
            string quizScore = File.ReadAllLines(".\\InputInfo.bin").Skip(1).Take(1).First();
            //sets a textbox equal to the string quizScore
            rchBxQuizNameScore.Text = quizScore;
        }

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

        //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))
                {
                    //adds the information from the textboxes to the file
                    scoreInfo.WriteLine(rchBxNameScore.Text + "\t" + rchBxQuizNameScore.Text + "\t" + rchBxScore.Text);
                }
                //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 saved as they get inputted.

k.m
  • 30,794
  • 10
  • 62
  • 86
Matty Dick
  • 11
  • 1
  • 7
  • Save your results in an array or `List` and then you can use `LINQ` and the `OrderByDescending` method. – Darren Feb 10 '15 at 18:37

1 Answers1

1

You need to store your scores in a List. Probably a List<int>. Then you can just use the OrderBy method:

IEnumerable<int> leaderboard = scores.OrderBy(x => x);

Or to get them descending:

IEnumerable<int> leaderboard = scores.OrderByDescending(x => x);

That gets you the list of scores sorted. Then using a collection view (like ListBox) you can display the list on your form.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117