Custom listbox sorting

前端 未结 2 1093
一向
一向 2020-12-12 02:35

I need to sort the data contained within a number of listboxes. The user will be able to select between two different types of sorting using radio boxes, one of which is che

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 03:24

    To change the sorting behaviour of a ListBox, you must implement your own ListBox class. I've written the solution for you and tested it.

    Right click on your project, select "Add Class..." and enter the class name "CustomListBox". Add the following code into the class between the namespaces:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;  // You need this namespace for ListBox
    
    public class CustomListBox : ListBox
    {
        public CustomListBox()
            : base()
        {
        }
    
        protected override void Sort()
        {
            if (this.Items.Count > 1)
            {
                bool swapped;
                do
                {
    
                    int counter = this.Items.Count - 1;
                    swapped = false;
    
                    while (counter > 0)
                    {
                        if (this.Items[counter].ToString().CompareTo(
                            this.Items[counter - 1].ToString()) == -1)
                        {
                            object temp = Items[counter];
                            this.Items[counter] = this.Items[counter - 1];
                            this.Items[counter - 1] = temp;
                            swapped = true;
                        }
    
                        counter -= 1;
    
                    }
    
                }
                while (swapped);
            }
        }
    }
    

    All you need to do with your sorting is to sort alphabetically. As per my example above, you can just use the CompareTo method to determine which string comes before or after another string.

    Now you've got your own custom ListBox, you can add additional properties and fields to it:

    public class CustomListBox : ListBox
    {
        public bool SortByJG;
    
        // Other code...
    }
    

    Then you can change the sort method to change the search based on the current sort mode as follows:

    while (counter > 0)
    {
        bool swap;
        if (this.SortByJG)
        {
            string[] breakDownCurrent = this.Items[counter].ToString().Split(' ');
            string[] breakDownPrevious = this.Items[counter - 1].ToString().Split(' ');
            if (breakDownCurrent[1].CompareTo(breakDownPrevious[1]) == -1)
            {
                swap = true;
            }
        }
        else
        {
            if (this.Items[counter].ToString().CompareTo(
                this.Items[counter - 1].ToString()) == -1)
            {
                swap = true;
            }
        }
    
        if (swap)
        {
            object temp = Items[counter];
            this.Items[counter] = this.Items[counter - 1];
            this.Items[counter - 1] = temp;
            swapped = true;
        }
    
        counter -= 1;
    
    }
    

    To make the sorting work, set Sorted = true; on the ListBox.

    UPDATE: Because the OP is struggling to understand, here's the entire class:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;  // You need this namespace for ListBox
    
    namespace WindowsApplication1
    {
    
        public class CustomListBox : ListBox
        {
    
            public CustomListBox()
                : base()
            {
            }
    
            public bool SortByJG;
    
            protected override void Sort()
            {
    
                if (this.Items.Count > 1)
                {
                    bool swapped;
                    do
                    {
    
                        int counter = this.Items.Count - 1;
                        swapped = false;
    
                        while (counter > 0)
                        {
                            bool swap = false;
                            if (this.SortByJG)
                            {
                                string[] breakDownCurrent = this.Items[counter].ToString().Split(' ');
                                string[] breakDownPrevious = this.Items[counter - 1].ToString().Split(' ');
                                if (breakDownCurrent[1].CompareTo(breakDownPrevious[1]) == -1)
                                {
                                    swap = true;
                                }
                            }
                            else
                            {
                                if (this.Items[counter].ToString().CompareTo(
                                    this.Items[counter - 1].ToString()) == -1)
                                {
                                    swap = true;
                                }
                            }
    
                            if (swap)
                            {
                                object temp = Items[counter];
                                this.Items[counter] = this.Items[counter - 1];
                                this.Items[counter - 1] = temp;
                                swapped = true;
                            }
    
                            counter -= 1;
    
                        }
    
    
                    }
                    while (swapped);
                }
            }
        }
    }
    

    You could now also add your own method to sort with the specified mode:

    public void Sort(bool sortByJG)
    {
       this.SortByJG = sortByJG;
       if (this.Sorted)
       {
          this.Sort();
       }
       else
       {
          this.Sorted = true;
       }
    }
    

提交回复
热议问题