0
        File.AppendAllText(path, (usernameforscore + " " + classicscore + Environment.NewLine));
        using (StreamReader reader = new StreamReader(path))
        {
            string line;
            while((line = reader.ReadLine())!=null)
            {
                if (line.IndexOf("Score") != -1) line = line.ToUpper();
                string[] sub = line.Split(" ");
                Console.WriteLine(line);
            }

I currently have this set of code to store and display a set of highscores, "usernameforscore" is a string previously inputted and "classicscore" is an integer previously calculated. Currently, it will write in as I want it to (for example - [Dom 9] would be in the file) , the display however just does it in the exact order of the file. My question is, is there a way to both order the outputting of these lines in a file by the highest "classicscore" variable.

Also, is there a way for it to also check for duplicate names before outputting it so it only gives the highest score of each person.

I'm using C# on Visual Studio.

  • 2
    Load them all into memory, and do your processing there before printing to screen. – Yair Halberstadt Nov 30 '18 at 10:55
  • 1
    `OrderBy` is your friend. – MakePeaceGreatAgain Nov 30 '18 at 10:56
  • FYI If this is a multi user environment your going to get file locking issues with this code. This is why people use databases to store data and not files – Liam Nov 30 '18 at 10:57
  • Possible duplicate of [How to sort an IEnumerable](https://stackoverflow.com/questions/3630687/how-to-sort-an-ienumerablestring) – Liam Nov 30 '18 at 10:58
  • *check for duplicate names* [`Dictionary` is your friend here.](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.7.2). Of course if you put this all into a database you could do all this using a single SQL statment – Liam Nov 30 '18 at 11:00
  • As far as filtering out duplicate highscores, you should probably replace a persons highscore when storing it inside the if they beat it, instead of writing another highscore. – Teun van der Wijst Nov 30 '18 at 11:01

0 Answers0