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

问题描述 投票:2回答:2

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

用户的姓名分数和时间保存在分数文本文件中,但要显示这些详细信息,我首先需要按分数订购这些详细信息。

我的测试文件组织如下:

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文件中对数据进行排序,这将是很好的。提前致谢。

c# text-files leaderboard
2个回答
1
投票

您可以使用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];``

0
投票

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

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

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
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.