如何按升序对列表框中的项目进行排序

时间:2012-09-19 16:27:23

标签: c# sorting listbox

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

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

FormItems类:

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

2 个答案:

答案 0 :(得分:2)

您可以使用LINQ:

直接在列表框中更新源项目或项目
list.OrderBy(x => x.Property)

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

答案 1 :(得分: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));
                    }
                }

            }