C# XNA从数组排序分数

C# XNA从数组排序分数,c#,arrays,sorting,xna,C#,Arrays,Sorting,Xna,我想为我的游戏创建一个高分板。 记分板包含文本文件中前5名的分数 文本文件如下所示: alpha, 3500 beta, 3600 gamma, 2200 delta, 3400 epsilon, 2000 这是我的密码: [Serializable] public struct HighScoreData { public string[] PlayerName; public int[] Score; public

我想为我的游戏创建一个高分板。 记分板包含文本文件中前5名的分数

文本文件如下所示:

alpha, 3500
beta, 3600
gamma, 2200
delta, 3400
epsilon, 2000
这是我的密码:

    [Serializable]
    public struct HighScoreData
    {
        public string[] PlayerName;
        public int[] Score; 

        public int Count;

        public HighScoreData(int count)
        {
            PlayerName = new string[count];
            Score = new int[count];

            Count = count;
        }

    }

    static HighScoreData highScores;
此代码用于从文本文件读取数据,并已在其中添加排序: 尝试 {

            using (StreamReader sr = new StreamReader("highscore.txt"))
            {

                string line;
                int i = 0;
                //file = new StreamReader(filePath);

                while ((line = sr.ReadLine()) != null)

                {

                    string[] parts = line.Split(',');                       
                    highScores.PlayerName[i] = parts[0].Trim();
                    highScores.Score[i] = Int32.Parse(parts[1].Trim());                       
                    i++;
                    Array.Sort(highScores.Score);
                }


            }


        }
我是这样画的:

        for (int i = 0; i < 5; i++)
        {
            spriteBatch.DrawString(spriteFont, i + 1 + ". " + highScores.PlayerName[i].ToString()
           , new Vector2(200, 150 + 50 * (i)), Color.Red);
            spriteBatch.DrawString(spriteFont, highScores.Score[i].ToString(),
                new Vector2(550, 150 + 50 * (i)), Color.Red);
        }

我必须做什么,这样程序才能对文本文件中的所有数据进行排序,而不仅仅是分数…?

生成一个名为PlayerScore的结构

struct PlayerScore 
{
    public string Player;
    public int Score;
    public int DataYouWant;

    public static int Compare(PlayerScore A, PlayerScore B)
    {
        return A.Score - B.Score;
    }
}
然后,要仅对Sort方法调用一次(在while之外),请按以下方式执行:

Array.Sort<PlayerScore>( yourArray, PlayerScore.Compare );
static PlayerScore[] highScores = new PlayerScore[MaxHighScorePlayers];

另一个不使用基于Blau样本的LINQ的比较器的选项:

struct PlayerScore
{
    public string Player;
    public int Score;
    public int DataYouWant;
}
然后是填充列表并对其排序的示例:

        List<PlayerScore> scores = new List<PlayerScore>();
        Random rand = new Random();
        for (int i = 0; i < 10; i++)
        {
            scores.Add(new PlayerScore()
            {
                Player = "Player" + i,
                Score = rand.Next(1,1000)
            });
        }
        scores = (from s in scores orderby s.Score descending select s).ToList();
        foreach (var score in scores)
        {
            Debug.WriteLine("Player: {0}, Score: {1}", score.Player, score.Score);
        }
列表分数=新列表();
Random rand=新的Random();
对于(int i=0;i<10;i++)
{
分数。添加(新玩家核心()
{
Player=“Player”+i,
分数=兰特下一步(11000)
});
}
分数=(按分数顺序从s开始。分数递减选择s)。ToList();
foreach(分数中的var分数)
{
WriteLine(“播放器:{0},分数:{1}”,Score.Player,Score.Score);
}
        List<PlayerScore> scores = new List<PlayerScore>();
        Random rand = new Random();
        for (int i = 0; i < 10; i++)
        {
            scores.Add(new PlayerScore()
            {
                Player = "Player" + i,
                Score = rand.Next(1,1000)
            });
        }
        scores = (from s in scores orderby s.Score descending select s).ToList();
        foreach (var score in scores)
        {
            Debug.WriteLine("Player: {0}, Score: {1}", score.Player, score.Score);
        }