Sorting an array that has numbers and text

Creating a simplistic high score save for a platformer. I am using the following code

bool score = System.IO.File.Exists("highscores.txt"); if (score) { highScore = System.IO.File.ReadAllLines("highscores.txt"); } else { string[] highScores = { "1000,AAA", "900,BBB", "800,CCC", "700,DDD", "600,EEE", "500,FFF", "400,GGG", "300,HHH", "200,III", "100,JJJ"}; System.IO.File.WriteAllLines("highscores.txt", highScores); System.IO.File.WriteAllLines("highscoresbackup.txt", highScores); highScore = highScores; } 

This sees if the txt file exists, if it doesn't, create one. If it does, read it. This is how it draws it

 for (int i = 0; i < 10; i++) { string name; string score; score = highScore[i].Split(',')[0].Trim(); name = highScore[i].Split(',')[1].Trim(); spriteBatch.DrawString(systemArialFont, name, new Vector2(startPosition.X, startPosition.Y + (i*30)), Color.White, 0, Vector2.Zero, 1.1f, SpriteEffects.None, 0); spriteBatch.DrawString(systemArialFont, score, new Vector2(startPosition.X + 100, startPosition.Y + (i * 30)), Color.White, 0, Vector2.Zero, 1.1f, SpriteEffects.None, 0); } 

This displays it how i want it, and works quite well. Finally when the player ends the game there is this

string[] highScoreInts = new string[11]; for (int c = 0; c < 10; c++) { highScoreInts[c] = highScore[c]; } highScoreInts[10] = GameHud.Score + "," + playerName; Array.Sort(highScoreInts); Array.Reverse(highScoreInts); string[] newScore = { highScoreInts[0] + "", highScoreInts[1] + "", highScoreInts[2] + "", highScoreInts[3] + "", highScoreInts[4] + "", highScoreInts[5] + "" , highScoreInts[6] + "", highScoreInts[7] + "", highScoreInts[8] + "", highScoreInts[9] + "" }; System.IO.File.WriteAllLines("highscores2.txt", newScore); System.IO.File.Replace("highscores2.txt", "highscores.txt", "highscoresbackup.txt"); saveScore = false; 

Which works quite well except one big flaw, a score such as 1000 will be placed above 100 / below 200, not above 900. I am having issues on figuring out how i would sort this. I thought about using the split function but i am a) unable to think of how to implement it, and b) afraid it would sort the numbers and not keep the names with it.

Any assistance on this would be greatly appreciated.

by CanBeUsedAnywhere via /r/csharp

Leave a Reply