Home > database >  How do I append the contents of a txt file into an array, but the txt file has different variable ty
How do I append the contents of a txt file into an array, but the txt file has different variable ty

Time:10-25

I'm not that great with coding and all since we've started learning the basics this year, and my groupmates aren't doing anything to help me, so I'm lost here and wanted to seek help from all the experts that are here.

I was wondering how I'd be able to sort the mixed data inside the Textbox properly (I'm using the console.application to access windows.Forms because that's what our professor wants us to do) . As you can see in the image below, it gets arranged alphabetically instead of sorting the score in descending order with the names of the player beside their scores.

void show_Click(object sender, EventArgs e)
        {
            int counter = 0;

            string line;

            StreamReader TXT = new StreamReader("Leaderboard.txt");
            List<string> list = new List<string>();
            while ((line = TXT.ReadLine()) != null)
            {
                ListB1.Items.Add(line);
                list.Add(line);
                counter  ;
            }
            string[] arr = list.ToArray();
            Array.Sort(arr);
            Array.Reverse(arr);
            foreach (string item in arr)
            {
                ListB2.Items.Add(item);
            }

this is barely a fraction of my whole code but this is the part I think is most essential to making my leaderboards function properly.

this is what my code results to, as of now...

CodePudding user response:

Currently you're reading each line of the file as a single string. So there's no difference between "score" and "text", it's all just one text value.

It sounds like what you want is to parse that information into an object with two different properties. For example, suppose you have this class:

public class ScoreListing
{
    public int Score { get; set; }
    public string Text { get; set; }
}

Then your list would be something like:

List<ScoreListing> list = new List<ScoreListing>();

And to add to it, you'd need to parse information from line to create a new ScoreListing object. For example, it might look something like this:

while ((line = TXT.ReadLine()) != null)
{
    var elements = line.Split(' | ');
    var listing = new ScoreListing
    {
        Score = int.Parse(elements[0]),
        Text = elements[1]
    };
    list.Add(listing);
    counter  ;
}

Once you have a populated List<ScoreListing> you can sort it with .OrderBy(), for example:

var sortedLeaderboard = list.OrderByDescending(l => l.Score);

There are multiple ways to approach the storage of the data, sorting the data, etc. But overall when you have a value like "123 | SomeText" then what you really have is a custom-serialized object with two distinct properties. Your first goal when reading this data into memory should be to de-serialize it into that meaningful object. Then you can easily use that object however you like.


By request, a bit more information on converting data back into a string...

The most object-oriented approach would be to override .ToString() on the class:

public class ScoreListing
{
    public int Score { get; set; }
    public string Text { get; set; }

    public override string ToString()
    {
        return $"{Score} | {Text}";
    }
}

Then you can add it to your list of strings:

foreach (var item in sortedLeaderboard)
{
    ListB2.Items.Add(item.ToString());
}

Without overriding .ToString() you'd just manually perform the same operation:

foreach (var item in sortedLeaderboard)
{
    ListB2.Items.Add($"{item.Score} | {item.Text}");
}
  • Related