简体   繁体   中英

How would I go about sorting a text file that contains names and scores in numerical order (Leaderboard style)

I'm creating a leaderboard system for a C# Console based quiz, I'm struggling to be able to sort the data stored within a text file so that it can be displayed with the highest scores at the top and the lowest at the bottom.

The text file formats entries as "Score - Name" (Without the quotations) One entry per line

12 - Daniel
14 - Greg
19 - Ben
6 - Samuel

Essentially, it should convert the text file which contains the text shown above to...

19 - Ben
14 - Greg
12 - Daniel
6 - Samuel

I'm not really sure where to start, I'm able to read in text files without issue using a StreamReader, my issue is sorting said data.

This is all I have, the beginnings of my leaderboard method.

        static void Leaderboard()
        {
            Console.Clear(); //Clears the console
            Console.WriteLine("======================================");
            Console.WriteLine("Quiz Leaderboard!");
            Console.WriteLine("Shown below are the top 10 users");
            Console.WriteLine("======================================");

            StreamReader lbfile = new StreamReader("../../../../leaderboard.txt");
        }

Once sorted, I will want to print out the results from the top 10 users into the console.

Edit: This is my first post on StackOverflow, I'm hoping I've done everything correctly, I'm happy to provide any additional information which would aid in creating a solution.

You can use the following, taking into consideration that there could be duplicates, it's not neat as I only had a few mins

string[] lines = System.IO.File.ReadAllLines(@"C:\TextFile1.txt");

        var dictionary = new Dictionary<int, List<string>>();

        foreach (var line in lines)
        {
            string[] vals = line.Split("-");

            var result = Convert.ToInt32(vals[0].Trim());
            var name = vals[1].Trim();

            if (!dictionary.ContainsKey(Convert.ToInt32(vals[0].Trim())))
            {
                dictionary.Add(result, new List<string> { new string(name) });
            }
            else
            {
                var duplicate = dictionary.GetValueOrDefault(result);
                duplicate.Add(name);
            }

        }

        var orderedList = dictionary.OrderByDescending(r => r.Key);


        Console.Clear(); //Clears the console
        Console.WriteLine("======================================");
        Console.WriteLine("Quiz Leaderboard!");
        Console.WriteLine("Shown below are the top 10 users");
        Console.WriteLine("======================================");

        foreach (var keyValuePair in orderedList)
        {

            Console.WriteLine(" Result = " + keyValuePair.Key);

            foreach (var val in keyValuePair.Value)
            {
                Console.WriteLine("     Name: " + val);
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM