使用特定方法对添加到列表的对象进行排序

时间:2015-03-03 16:29:18

标签: c# list sorting inheritance

我正在尝试将已添加的项目排序到添加列表项后列表框中显示的列表中。 (我使用了继承 - 车辆类(基础类)和汽车,卡车和摩托车是派生类)。

问题是 - 我收到了一种方法,通过该方法,排序过程必须遵循(或我必须使用的)排序列表框中显示的添加项目。有人可以解释一下如何在我的主要表格列表中实现这个方法吗?

我有以下代码:

主要形式:

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

由于

Ĵ

2 个答案:

答案 0 :(得分:2)

假设您的mySort()方法已经完成(因为您说它是给您的),您应该将所有车辆添加到_VList,而不是使用myVehicle。这样您就可以拨打myVehicle.mySort()

答案 1 :(得分:0)

使用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)); 
        });
    }
}