locked
Sorting a List and then Displaying it in a Listbox. RRS feed

  • Question

  • Hello, I am currently working on a project that requires me to sort out a list and display it in a listbox all sorted.
    I've tried lots of things such as animals.Sorted() - it throws up an error and listboxanimal.Sorted = true, this works but then all my data is messed up because, well am guessing you know why am what am saying...

    Code:

    List<NamedAnimal> animals = new List<NamedAnimal>() { new Dog(), new Cat(), new Horse(),  new Cow(), new Duck() };

            private void AnimalForm_Load(object sender, EventArgs e)
            {
                for (int i = 0; i < animals.Count; i++)
                {
                    listBoxAnimal.Items.Add(animals[i].Name);

                }
            }

    • Moved by CoolDadTx Monday, March 31, 2014 4:00 PM Winforms related
    Monday, March 31, 2014 2:54 PM

Answers

  •     class NamedAnimal : Animal, IComparable<NamedAnimal>
        {
            string name;

            public int CompareTo(NamedAnimal namedAnimal)
            {
                return this.Name.CompareTo(namedAnimal.name);
            }

    Sorted

    • Marked as answer by AlotaVagina Tuesday, April 1, 2014 1:28 AM
    Tuesday, April 1, 2014 1:28 AM

All replies

  • Hi,

    Instead of Sorting the data on your own, you can do the following.

    1. Add the items into the list box

    2. Set the "Sorted" property of the listbox to "True".

    -Santhosh N


    • Edited by santhoshnagarajan Monday, March 31, 2014 3:22 PM screenshot added
    • Proposed as answer by CoolDadTx Monday, March 31, 2014 3:59 PM
    Monday, March 31, 2014 3:21 PM
  • I tend to agree with Santhosh that the listbox property is the correct solution.  But I will warn you that it slows down the listbox when inserting items.  For a small # of items you won't notice it but if you're putting a lot of items in the listbox at once (aka data binding) then it is noticeably slow.  Also note that it uses the text that will be displayed for sorting and not what you might want to sort by.  For example if you were displaying a list of countries but you wanted your country at the top then the LB property won't help you.

    If you need to sort a list of items, in general, then you can use LINQ

    var sorted = (from a in animals order by a.Name);  //Sorts by the Name property

    You can actually sort by any combination of properties and ascending or descending.  The advantage here is that you have far more flexibility in sorting.  The disadvantage is that if you need to insert an item into the listbox later then you'll have to figure out where to put it.

    Also you are manually binding your list.  A quicker (more efficient way) to bind to a listbox is to use the DataSource property and the DisplayMember, ValueMember properties.

    listboxAnimal.DisplayMember = "property that contains the name to display";
    listboxAnimal.ValueMember = "property that contains the value, if any";
    listboxAnimal.DataSource  = animals;  //Binds all items at once without a foreach loop

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Monday, March 31, 2014 4:06 PM
  • List box Sorted doesn't work because when I click on the list, such as dog, the list in a list box is sorted but the list behind it isn't, so Ill click on dog and get cat...

    var sorted = (from a in animals order by a.Name);  //Sorts by the Name property

    No matter how i try and write this it doesn't work...

    My teacher says


    listboxAnimal.DisplayMember = "property that contains the name to display";
    listboxAnimal.ValueMember = "property that contains the value, if any";
    listboxAnimal.DataSource  = animals;  //Binds all items at once without a foreach loop

    This is to advanced and is for later so... Am stuck :(

    Tuesday, April 1, 2014 12:19 AM
  •     class NamedAnimal : Animal, IComparable<NamedAnimal>
        {
            string name;

            public int CompareTo(NamedAnimal namedAnimal)
            {
                return this.Name.CompareTo(namedAnimal.name);
            }

    Sorted

    • Marked as answer by AlotaVagina Tuesday, April 1, 2014 1:28 AM
    Tuesday, April 1, 2014 1:28 AM