排序文本文件并输入到列表框 c#

问题描述

我正在尝试制作一个排行榜,根据每个参与者的得分对他们进行排序。 每个用户都会说他们是输了还是输了,然后代码会给出输赢的数字。 然后将其添加到列表框并保存在 .txt 文件中。 我的问题是我想根据他们的得分对排行榜进行排序。 以下是文件中的所有代码,以便您知道我在使用什么

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.
    }

 }
}

我知道有很多,但上下文非常重要。任何帮助都会很棒。如果我可以在保存到 .txt 文件之前对列表框进行排序,那也可以。

解决方法

我认为你需要一个课程

    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}";
        }

    }

例如

    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();

    }
,

试试这个:

            //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);