0

我有一个填充了下面代码的列表框,但我希望对其中的项目进行排序。如何对列表框中的项目进行排序

public void refreshInterface()
{
    foreach (DataSet1.xspGetAnalysisUsageTypesRow homeForms in myDataSet.xspGetAnalysisUsageTypes)
    {
        var forms = new FormItems(homeForms);
        listBox1.Items.Add(forms);
    }
}

表单项类:

public class FormItems
{
    public DataSet1.xspGetAnalysisUsageTypesRow types { get; set; }

    public FormItems(DataSet1.xspGetAnalysisUsageTypesRow usageTypes)
    {
        types = usageTypes;
    }

    public override string ToString()
    {
        return this.types.xlib_Desc;
    }
}
4

2 回答 2

2

您可以使用 LINQ 直接更新源项目或列表框上的项目:

list.OrderBy(x => x.Property)

您可以在其中将 x.Property 设置为要排序的属性。这种排序将默认为您想要的升序。否则只需使用OrderByDescending()

于 2012-09-19T16:40:59.977 回答
0

这是我对 LisBox 项目 A->Z 或 Z->A 进行排序的解决方案:

public void Sorting(ListBox lst, String order) {
                 if (order.Equals("A-Z"))
                {
                    if (lst.Items.Count > 1)
                    {
                        bool swapped;
                        do
                        {
                            int counter = lst.Items.Count - 1;
                            swapped = false;

                            while (counter > 0)
                            {
                                // Compare the items 
                                if (lst.Items[counter].ToString().CompareTo(lst.Items[counter -
    1].ToString()) < 0)
                                {
                                    // Swap the items.
                                    object temp = lst.Items[counter];
                                    lst.Items[counter] = lst.Items[counter - 1];
                                    lst.Items[counter - 1] = temp;
                                    swapped = true;
                                }
                                // Decrement the counter.
                                counter -= 1;
                            }
                        }
                        while ((swapped == true));
                    }
                }
                if (order.Equals("Z-A")) {
                    if (lst.Items.Count > 1)
                    {
                        bool swapped;
                        do
                        {
                            int counter = lst.Items.Count - 1;
                            swapped = false;

                            while (counter > 0)
                            {
                                // Compare the items 
                                if (lst.Items[counter].ToString().CompareTo(lst.Items[counter -
    1].ToString()) > 0)
                                {
                                    // Swap the items.
                                    object temp = lst.Items[counter];
                                    lst.Items[counter] = lst.Items[counter - 1];
                                    lst.Items[counter - 1] = temp;
                                    swapped = true;
                                }
                                // Decrement the counter.
                                counter -= 1;
                            }
                        }
                        while ((swapped == true));
                    }
                }

            }
于 2016-02-26T02:06:04.620 回答