MFJones
MFJones

Reputation: 39

Sorting objects added to list with a specific method

I'm trying to sort items I've already added into a list that appears on a listbox after the list items have been added. (I made use of inheritance - Vehicle Class (Base class) and Car, Truck and Motorcycle are derived classes).

The problem is - I received a method by which the sorting procedure must adhere to (or what I must use) to sort the added items displayed in the listbox. Can someone please explain how I can implement this method on my list in my main form please?

I have the following code:

Main form:

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

The method I must use is in my VehicleList class and looks like this:

class VehicleList : List<Vehicle>
{
    public void mySort()
    {
        this.Sort(delegate(Vehicle p1, Vehicle p2)
        {
            return p1.VehicleRegistration.CompareTo(p2.VehicleRegistration);
        });
    }
}

Thanks

J

Upvotes: 1

Views: 68

Answers (2)

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

Demo using a Func delegate to sort your list:

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

Upvotes: 0

PiousVenom
PiousVenom

Reputation: 6908

Assuming that your mySort() method is already complete(since you say it was given to you) you should, instead of using _VList, add all of your vehicles to your myVehicle. This will allow you to call the myVehicle.mySort().

Upvotes: 2

Related Questions