Custom listbox sorting

前端 未结 2 1076
一向
一向 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:08

    The first algorithm is the same as alphabetic ordering, so you can use directly:

    int res = string.Compare(first,second);
    

    The second algorithm is descending alphabetic ordering of the last two chars:

    int res = -string.Compare(first.Substring(first.Length - 2, 2), second.Substring(first.Length - 2, 2));
    

    To sort the list you have two options; the first is to create your own ListBox subclass and override the Sort method as detailed on the MSDN page for ListBox.Sort Method.

    The second (easier and uglier) is to put all the items in a collection, order the collection and replace the items in the list, something like this:

    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1 {
        public partial class Form1 : Form {
    
            void SortListBox() {
                List<string> items = new List<string>();
                foreach (string value in listBox1.Items) {
                    items.Add(value);
                }
                items.Sort((first, second) => string.Compare(first, second));
                listBox1.Items.Clear();
                listBox1.Items.AddRange(items.ToArray());
            }
    
        }
    }
    

    Hope this helps...

    0 讨论(0)
  • 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;
       }
    }
    
    0 讨论(0)
提交回复
热议问题