使用特定方法对添加到列表中的对象进行排序
本文关键字:对象 排序 列表 方法 添加 | 更新日期: 2024-09-25 10:51:31
我正在尝试对已添加到列表中的项目进行排序,该列表在添加列表项目后显示在列表框上。(我使用了继承-车辆类(基类)和汽车、卡车和摩托车是派生类)。
问题是,我收到了一种排序程序必须遵循的方法(或者我必须使用的方法),以对列表框中显示的添加项目进行排序。有人能解释一下我如何在主表格中的列表中实现这个方法吗?
我有以下代码:
主要形式:
namespace 3
{
public partial class Form1 : Form
{
VehicleList _VList = new VehicleList();
public Form1()
{
InitializeComponent();
}
//Add a vehicle to the list using inheritance
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
Car myCar = new Car();
myCar.VehicleRegistration = textBox1.Text;
myCar.EngineNumber = textBox2.Text;
myCar.Make = textBox3.Text;
myCar.Model = textBox4.Text;
myCar.EngineSize = Convert.ToInt32(textBox5.Text);
myCar.Colour = textBox6.Text;
//Add car to the list
_VList.Add(myCar);
//Display list
listBox1.Items.Add(myCar.ToString());
//Clear textboxes
comboBox1.ResetText();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
}
else if (comboBox1.SelectedIndex == 1)
{
Truck myTruck = new Truck();
myTruck.VehicleRegistration = textBox1.Text;
myTruck.EngineNumber = textBox2.Text;
myTruck.Make = textBox3.Text;
myTruck.Model = textBox4.Text;
myTruck.EngineSize = Convert.ToInt32(textBox5.Text);
myTruck.LoadCapacity = Convert.ToInt32(textBox6.Text);
//Add truck to list
_VList.Add(myTruck);
//Display list
listBox1.Items.Add(myTruck.ToString());
//Clear textboxes
comboBox1.ResetText();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
}
else if (comboBox1.SelectedIndex == 2)
{
Motorbike myMotorbike = new Motorbike();
myMotorbike.VehicleRegistration = textBox1.Text;
myMotorbike.EngineNumber = textBox2.Text;
myMotorbike.Make = textBox3.Text;
myMotorbike.Model = textBox4.Text;
myMotorbike.EngineSize = Convert.ToInt32(textBox5.Text);
myMotorbike.TopSpeed = Convert.ToDouble(textBox6.Text);
//Add motorbike to list
_VList.Add(myMotorbike);
//Display list
listBox1.Items.Add(myMotorbike.ToString());
//Clear textboxes
comboBox1.ResetText();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
}
}
private void button2_Click(object sender, EventArgs e)
{
//Sort the items in the list when the sort button is pressed
//HOW do I apply this method to my list?
_VList.mySort();
}
}
}
我必须使用的方法在VehicleList类中,如下所示:
class VehicleList : List<Vehicle>
{
public void mySort()
{
this.Sort(delegate(Vehicle p1, Vehicle p2)
{
return p1.VehicleRegistration.CompareTo(p2.VehicleRegistration);
});
}
}
感谢
J
假设您的mySort()
方法已经完成(因为您说它是给您的),您应该将所有车辆添加到myVehicle
中,而不是使用_VList
。这将允许您调用myVehicle.mySort()
。
使用Func委托对列表进行排序的演示:
public partial class Form1 : Form
{
private VehicleList vehicleList = new VehicleList
{
new Vehicle{ VehicleRegistration = "b"},
new Vehicle{ VehicleRegistration = "a"}
};
public Form1()
{
InitializeComponent();
listBox1.DataSource = vehicleList;
}
private void btnSort_Click(object sender, EventArgs e)
{
vehicleList.SortBy(x => x.VehicleRegistration);
UpdateListBox();
}
private void UpdateListBox()
{
listBox1.DataSource = null; //clear items
listBox1.DataSource = vehicleList; //reset
}
}
class VehicleList : List<Vehicle>
{
public void SortBy(Func<Vehicle, IComparable> theThingToCompare)
{
this.Sort(delegate(Vehicle v1, Vehicle v2)
{
return theThingToCompare(v1).CompareTo(theThingToCompare(v2));
});
}
}