将文本文件排序并输入列表框c#
我试图建立一个领导者委员会,根据每个参与者的得分对他们进行排序。 每个用户都会说他们是输了还是赢了,然后代码会给出赢或输的数字。 然后将其添加到列表框并保存在.txt文件中。 我的问题是我想根据他们的得分对领导委员会进行排序。 下面是文件中的所有代码,以便您了解我的工作内容将文本文件排序并输入列表框c#,c#,sorting,listbox,leaderboard,C#,Sorting,Listbox,Leaderboard,我试图建立一个领导者委员会,根据每个参与者的得分对他们进行排序。 每个用户都会说他们是输了还是赢了,然后代码会给出赢或输的数字。 然后将其添加到列表框并保存在.txt文件中。 我的问题是我想根据他们的得分对领导委员会进行排序。 下面是文件中的所有代码,以便您了解我的工作内容 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Dra
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();
}
static void Main(字符串[]args)
{
var testList=新列表()
{
“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=>newuser(m));
users=users.OrderBy(m=>m.Result.ToList();
//或
//users=users.OrderByDescending(m=>m.Result.ToList();
foreach(用户中的var用户)
{
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);
//用测试数据填充列表框
var temp=Enumerable.Range(1100)。选择(x=>(对象)字符串。连接(“|”,新对象[]{“A”,“B”,“C”,x.ToString()})).ToArray();
listBox1.Items.AddRange(温度);
var sortedRows=listBox1.Items.Cast().OrderBy(x=>int.Parse(x.Split(new char[]{'.}).Last()).ToArray();
listBox1.Items.Clear();
列表框1.Items.AddRange(sortedRows);
在排序之前,您需要将文本编号解析为一个数字。我应该在什么时候执行此操作,您可以告诉我排序的代码吗?这将对我的列表框或文本文件进行排序,因为我不知道在何处放置代码而不会出现错误。因此,我如何按此排序顺序将此数据添加到列表框?我无法按需要清除列表框所有预先存在的数据以及新的信息,只要你的代码使列表框中充满了A¦B¦C¦1、A¦B¦C¦2等等,代码就是用排序的相同项替换当前项。添加新项目后,必须重新排序。我只是为了测试而创建数据来填充列表框。那么我该如何调整代码以使其输入我的文本文件呢。我用你的代码填充列表框,然后用我的新代码排序。