[英]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
. 我试图在richTextBox
中获得richTextBox
排序的测验分数。 I save the players name, quiz and score in a class called scoresClass
. 我在名为scoresClass
的类中保存玩家的姓名,测验和分数。 At the end of the quiz, I call the class to three richTextBox
s, to show their name, quiz and score. 在测验的最后,我将类称为三个richTextBox
,以显示其名称,测验和分数。 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. 排行榜(即richTextBox
)设置为等于文件中的数据。
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
. 由于您是使用StreamWriter
附加到文件的,所以我将文件作为scoreClass
的集合读回,而不是盲目地读取文件并将其转储到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. 将所有乐谱存储在内存中后,您便可以进行一些LINQ
工作来对它们进行排序。 You can then iterate through each value in the List<scoresClass>
and export to the richTextBox
's as desired. 然后,您可以遍历List<scoresClass>
中的每个值,并根据需要导出到richTextBox
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.