newbie: sort values in a text.txt and display by name?
-
Hi! I have a textfile which contains Name and Value (each name has a value). I want to display ONLY the names in a listbox. And it should be the names with the 10 highest scores. The highest score first... I do know how to fill the listbox with all the names. But I cant get around to the other...any suggestions?
Newbie untill I die! :-)
-
Hi! I have a textfile which contains Name and Value (each name has a value). I want to display ONLY the names in a listbox. And it should be the names with the 10 highest scores. The highest score first... I do know how to fill the listbox with all the names. But I cant get around to the other...any suggestions?
Newbie untill I die! :-)
Show us how your txt is formatted If it's something like "name=value" then you could do something like that a) string[] lines = File.ReadAllLines("highscores.txt") b) create a class:
struct Highscore
{
public string name;
public int score;
}b)
List<Highscore> scores = new List<Highscore>();
foreach line in lines {
Highscore h = new Highscore();
h.name = line.Substring(0, line.IndexOf('='));
h.score = int.Parse(line.Substring(line.IndexOf('=')+1));
scores.Add(h);
}c)
scores.Sort(... some IComparer to sort for score ...)
d)
for(int i=0; i<10; i++)
listbox.Add(scores[i].name);regards
-
Show us how your txt is formatted If it's something like "name=value" then you could do something like that a) string[] lines = File.ReadAllLines("highscores.txt") b) create a class:
struct Highscore
{
public string name;
public int score;
}b)
List<Highscore> scores = new List<Highscore>();
foreach line in lines {
Highscore h = new Highscore();
h.name = line.Substring(0, line.IndexOf('='));
h.score = int.Parse(line.Substring(line.IndexOf('=')+1));
scores.Add(h);
}c)
scores.Sort(... some IComparer to sort for score ...)
d)
for(int i=0; i<10; i++)
listbox.Add(scores[i].name);regards
peter 0,333333333333333 peter 1,33333333333333 thats how its look like
Newbie untill I die! :-)