2015-02-10 6 views
-2

Я пытаюсь поместить десятки моих лидеров в порядок от наивысшего до самого низкого. Я не уверен, как я это сделаю, я знаю, что это не место, чтобы попросить написать код, но я везде искал, мне нужно создать массив или список или класс?Лидер рейтинга

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

     } 
    } 
} 

В настоящее время оценки сохраняются по мере их ввода.

+0

Сохраните результаты в массиве или 'List', а затем вы можете использовать' LINQ' и метод 'OrderByDescending'. –

ответ

1

Вам необходимо сохранить ваши результаты в List. Вероятно, List<int>. Тогда вы можете просто использовать OrderBy метод:

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

Или получить их по убыванию:

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

Это получает вам список оценок, отсортированных. Затем, используя представление коллекции (например, ListBox), вы можете отобразить список в своей форме.

Смежные вопросы