繁体   English   中英

如何在c#中按高分对存储在文本文件中的数据进行排序

[英]How to sort data stored in a text file by high score in c#

我正在为我的计算课程制作一个测验游戏,我已经完成了我的应用程序的测验部分,我目前正在进入我的排行榜,但要做到这一点,我需要对我的分数文本文件进行排序,其中包含用户名、分数和时间,但我现在正在尝试检索这些数据并将它们显示在表格中。

用户的姓名分数和时间可以很好地保存到分数文本文件中,但要显示这些详细信息,我首先需要按分数对这些详细信息进行排序。

我的测试文件是这样组织的:

username,score,time

userName1,33,12
userName2,-55,33
userName3,34,2
userName4,23,27
userName5,63,72

这是我目前正在使用的代码,但这仅在我首先对文本文件中的数据进行排序时才有效。

string[] readFile = File.ReadAllLines(file).ToArray();

for (int i = 0; i < 5; i++)
{
    string[] userDetails = File.ReadAllLines(file).ToArray();

    string username = userDetails[0];
    string score = userDetails[1];

    // Apply the text of lblUsername1-5 to be what the names 
    // of the top 5 scorers are in the file.
    lblUsername1.Text = userDetails[0].Split(',')[0];
    lblUsername2.Text = userDetails[1].Split(',')[0];
    lblUsername3.Text = userDetails[2].Split(',')[0];
    lblUsername4.Text = userDetails[3].Split(',')[0];
    lblUsername5.Text = userDetails[4].Split(',')[0];

    // Apply the text of lblScore1-5 to be what the scores 
    // of the top 5 scorers are in the file.
    lblScore1.Text = userDetails[0].Split(',')[1];
    lblScore2.Text = userDetails[1].Split(',')[1];
    lblScore3.Text = userDetails[2].Split(',')[1];
    lblScore4.Text = userDetails[3].Split(',')[1];
    lblScore5.Text = userDetails[4].Split(',')[1];
}

因此,如果有人可以帮助我对分数 ext 文件中的数据进行排序,那就太好了。 提前致谢。

您可以使用 linq 对文件中的数据进行排序

string[][] userDetails = File.ReadAllLines(file).Select(s => s.Split(',')).OrderBy(arr => int.TryParse(arr[1], out int result) ? result : 0)).Take(5).ToArray();

lblUsername1.Text = userDetails[0][0];
lblUsername2.Text = userDetails[1][0];
lblUsername3.Text = userDetails[2][0];
lblUsername4.Text = userDetails[3][0];
lblUsername5.Text = userDetails[4][0];

// Apply the text of lblScore1-5 
// to be what the scores of the top 5 scorers are in the file.
lblScore1.Text = userDetails[0][1];
lblScore2.Text = userDetails[1][1];
lblScore3.Text = userDetails[2][1];
lblScore4.Text = userDetails[3][1];
lblScore5.Text = userDetails[4][1];``

您应该使用对象来管理它。 您的班级应该是具有属性的用户,如下所示。

现在您可以完全控制对象的排序和管理

using System.Collections.Generic;
using System.Linq;

public class User
{
    public string Name { get; set; }
    public int Score { get; set; }
    public int Time { get; set; }
}

class Program
{
    public static void Main(string[] args)
    {
        //This you get from file, no need for this in your code            
        string[] fromFile = new string[5] 
        { "userName1,33,12", "userName2,-55,33", "userName3,34,2", "userName4,23,27", "userName5,63,72" };    

        List<User> users = new List<User>();

        foreach (string line in fromFile)
        {
            string[] splitLine = line.Split(',');
            users.Add(new User() { Name = splitLine[0], Score = int.Parse(splitLine[1]), Time = int.Parse(splitLine[2]) });    
        }    

        foreach (User oneUser in users.OrderByDescending(x => x.Score))
        {
            //Here the store to file or what you want to do
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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