-1

I'm trying to make a leader board that sorts each participant by the score they get. Each user will say whether they have one or lost then the code gives the win or loss a number. This then gets added to a listbox and is saved in a .txt file. My issue is i want to sort the leader board by what they've scored. Below is all the code from the file just so you know what I'm working with

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
 public partial class BadForm : Form
 {
    public BadForm()
    {
        InitializeComponent();
    }

    private void BackBut_Click(object sender, EventArgs e)
    {
        this.Hide();
        TeaEveSel f4 = new TeaEveSel();
        f4.Show();
        // This shows the team event selection page when the button is pressed
    }

    private void HomeBut_Click(object sender, EventArgs e)
    {
        this.Hide();
        StartForm f4 = new StartForm();
        f4.Show();
        // This shows the start up page when the button is pressed
    }

    private void TeaSave_Click(object sender, EventArgs e)
    {
        if (ResultsBox.Text == "Win")
        {
            ResultsBox.Text = 5.ToString(); // Whenever win is selected it changes to the number 5 so that calculations can be done
        }
        else if (ResultsBox.Text == "Lose")
        {
            ResultsBox.Text = 1.ToString(); // Whenever lose is selected it changes to the number 1 so that calculations can be done
        }
        else
        {

        }

        listBox1.Items.Add(string.Format("{0} | {1} | {2} | {3}", IndNamBox.Text, TeaNamBox.Text, EveNamBox.Text, ResultsBox.Text));
        listBox1.Items.Add("");
        //This adds the text in the combo boxes and text boxes to the list box



    }

    private void TeaTable_Click(object sender, EventArgs e)
    {
        const string fpath = "G:\\IT\\unit 4\\assignment 2\\WindowsFormsApp1\\Badminton.txt";

        var SaveFile = new System.IO.StreamWriter(fpath);

        foreach (var item in listBox1.Items)
        {
            SaveFile.WriteLine(item.ToString());
        }
        SaveFile.ToString();
        SaveFile.Close();
        //When this button is pressed it updates the txt file with the text in the list box

        BigServer frm2 = new BigServer();
        frm2.Show();
        frm2.Hide();
        //This opens and closes the BigServer form so that the listbox on the BigServer loads the txt file 
    }

    private void BadForm_Load(object sender, EventArgs e)
    {
        var lines = File.ReadAllLines("G:\\IT\\unit 4\\assignment 2\\WindowsFormsApp1\\Badminton.txt");
        listBox1.Items.AddRange(lines);
        //This fills the listbox with the txt file so that all the information is in the file and nothing gets overwritten.
    }

 }
}

I know there's a lot but The context is very important. Any help would be great. If i could sort the listbox before saving to the .txt file then that would also work.

2 Answers2

0

Hy i think you need a class for that

    public class User
    {
        public User()
        {

        }

        public User(string yourString)
        {
            var result = yourString.Split('|').ToList();
            if (result.Count == 4)
            {
                Name = result[0];
                Team = result[1];
                Event = result[2];
                Result = int.Parse(result[3]);
            }
        }

        public string Name { get; set; }
        public string Team { get; set; }
        public string Event { get; set; }
        public int Result { get; set; }

        public override string ToString()
        {
            return $"{Name}|{Team}|{Event}|{Result}";
        }

    }

for example

    static void Main(string[] args)
    {
        var testList = new List<string>()
        {
            "a|b|c|5",
            "a1|b1|c1|4",
            "a2|b2|c2|3",
            "a3|b4|c5|1",
            "a5|b5|c5|7",
        };

        var users = testList.Select(m => new User(m));

        users = users.OrderBy(m => m.Result).ToList();
        //or
        //users = users.OrderByDescending(m => m.Result).ToList();

        foreach (var user in users)
        {
            Console.WriteLine(user.ToString());
        }


        Console.ReadLine();

    }
0

Try this :

            //fill listbox with test data
            var temp = Enumerable.Range(1,100).Select(x => (object)string.Join(" | ", new object[] { "A","B","C",x.ToString()})).ToArray();
            listBox1.Items.AddRange(temp);  

            var sortedRows = listBox1.Items.Cast<string>().OrderBy(x => int.Parse(x.Split(new char[] {'|'}).Last())).ToArray();
            listBox1.Items.Clear();
            listBox1.Items.AddRange(sortedRows);
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • i can't clear the listbox as it needs to all pre-existing data as well as new information – Dafydd Volk-Evans May 11 '21 at 14:25
  • slight issue your code just makes the list box filled with A¦B¦C¦1, A¦B¦C¦2 and so on – Dafydd Volk-Evans May 11 '21 at 15:03
  • The code is just replacing current items with same items sorted. After you add new items you have to sort again. I just created data to fill listbox just for testing. – jdweng May 11 '21 at 15:32
  • so how would I adjust the code so that it inputs my text file – Dafydd Volk-Evans May 13 '21 at 08:38
  • You code is fine. I used your code to fill listbox and then sorted with my new code. – jdweng May 13 '21 at 09:32
  • ok but I have used a listbox but i can't sort the data in it how do i use your code to sort my data as yours just came up with the bloody alphabet i am just trying to sort the code by the numbers at the end – Dafydd Volk-Evans May 13 '21 at 12:54
  • That is what my code does. I tested and it works. Are you sure code doesn't work? – jdweng May 13 '21 at 13:00
  • no the code works it just doesn't input what i am looking for it is inputting your test. i want it to sort the data from my text file but instead it is inputting your random data can you show the code so that it will work in context. – Dafydd Volk-Evans May 13 '21 at 14:00
  • I need to see sample of text. I used the format from your line of code : listBox1.Items.Add(string.Format("{0} | {1} | {2} | {3}", IndNamBox.Text, TeaNamBox.Text, EveNamBox.Text, ResultsBox.Text)); – jdweng May 13 '21 at 14:06
  • dafydd | Dark-Thunder | Badminton | 5 That's the text file results also i have remove the IndNamBox.Text as was useless – Dafydd Volk-Evans May 13 '21 at 14:40
  • I do not see why the code doesn't work. I'm splitting on the '|' character and then parsing the last item in the split to a number and then ordering. Just tested code again and it works. – jdweng May 13 '21 at 14:50
  • i mean it works it just isn't producing the correct result – Dafydd Volk-Evans May 13 '21 at 14:53
  • There must be something unusual with the data. Maybe some lines have a different number of '|'. If the last split item wasn't an integer there would be an exception. Are yousure you a executing the click? May be adding more data after the sort? – jdweng May 13 '21 at 15:01