|
-
Dec 15th, 2009, 05:09 PM
#1
Thread Starter
Hyperactive Member
Sorting a List of strings
Hi. I'm trying to sort my list of strings by a #'s that occur near the end of the string.
Example:
PLAYER_NAME: Temp PLAYER_SCORE: 1
PLAYER_NAME: Temp PLAYER_SCORE: 7
PLAYER_NAME: Temp PLAYER_SCORE: 4
Now I want it to appear like this on my output:
Name: Temp Score: 7
Name: Temp Score: 4
Name: Temp Score: 1
I have it outputting properly expect its not sorting my Score.
Here is my current code:
Note, I'm using XNA 3.1 but this is WinForm Code for Reading and Sorting.
Code:
string line;
string leaderName = string.Empty;
string leaderScore = string.Empty;
int lastScore = 0;
int currentScore = 0;
List<string> Leaderboard = new List<string>();
UIYourScore = new UIClass();
UIYourScore.FontType = Game.Content.Load<SpriteFont>(@"UIScoreFont");
UIYourScore.Value = Player.Score;
UIYourScore.String = "Your Score: " + UIYourScore.Value.ToString();
UIYourScore.Position = new Vector2(250, 150);
UIBestScore = new UIClass();
UIBestScore.FontType = Game.Content.Load<SpriteFont>(@"UIScoreFOnt");
File.AppendAllText(StorageContainer.TitleLocation + @"\Leaderboard.txt",
"PLAYER_NAME: Temp" + Environment.NewLine +
"PLAYER_SCORE: " + Player.Score.ToString() + Environment.NewLine);
TextReader tr = new StreamReader(StorageContainer.TitleLocation + @"\Leaderboard.txt");
while ((line = tr.ReadLine()) != null)
{
if (line.Contains("PLAYER_NAME: "))
{
line = line.Remove(0, 13);
leaderName = line;
}
if (line.Contains("PLAYER_SCORE: "))
{
line = line.Remove(0, 14);
lastScore = int.Parse(line);
leaderScore = lastScore.ToString();
if (currentScore <= lastScore)
{
currentScore = lastScore;
UIBestScore.Value = currentScore;
}
Leaderboard.Add("Name: " + leaderName + "\t Score: " + leaderScore);
}
}
tr.Close();
UIBestScore.String = "Best Score: " + UIBestScore.Value.ToString();
UIBestScore.Position = new Vector2(250, 175);
UILeaderboard = new UIClass[Leaderboard.Count - 1];
for (int i = 0; i < Leaderboard.Count - 1; i++)
{
UILeaderboard[i] = new UIClass();
UILeaderboard[i].FontType = Game.Content.Load<SpriteFont>(@"UIScoreFont");
UILeaderboard[i].String = Leaderboard[i] + Environment.NewLine;
if (i == 0)
UILeaderboard[i].Position = new Vector2(800, 150);
else
UILeaderboard[i].Position = new Vector2(800, UILeaderboard[i - 1].Position.Y + 20);
}
-
Dec 15th, 2009, 08:26 PM
#2
Re: Sorting a List of strings
Follow the blog link in my signature and check out my posts on sorting lists. In your case your comparer method would use either a Regex or string manipulation (IndexOf, Substring) to get the number at the end of the string and then convert that to an Integer for comparison.
-
Dec 15th, 2009, 10:25 PM
#3
Thread Starter
Hyperactive Member
Re: Sorting a List of strings
Alright. I was thinking something a long those lines like that.
Get the IdexOf the String, Substring it, Sort the Highest # first, then do another READ of the file to match # highest to the name of the Person ?
Is there another way to match my Sorted Highest # after I substring to match the same Person's Name ?
-
Dec 15th, 2009, 10:30 PM
#4
Re: Sorting a List of strings
There's no need to read the file twice. You read it once into a string array and then you sort that string array. That's it.
If you actually need the separate parts of the data then I would suggest that you define a type that represents one record, with properties for name, score and whatever else. You can then create an array of instances of that type from the data in your string array and then you can sort that new array by the score property.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|