开发者

Sorting a list of items in a list box

开发者 https://www.devze.com 2023-01-15 12:16 出处:网络
I want to get an bunch of items from a list box, add them to an array, sort it, then put it back into a different listbox. Here is what I have came up with:

I want to get an bunch of items from a list box, add them to an array, sort it, then put it back into a different listbox. Here is what I have came up with:

ArrayList q = new ArrayList();
        foreach (object o in listBox4.Items)
            q.Add(o);
        q.Sort();
        listBox5.Items.开发者_StackOverflowAdd(q.ToString());

But it doesnt work. Any ideas?


You could just use the ListBox.Sorted built in functionality

  foreach (object o in listBox4.Items)
  {
    listBox5.Items.Add(o);
  }
  listBox5.Sorted = true;

Setting ListBox5.Sorted=true will ensure that the items in the listbox are sorted and any subsequent items added to the listbox will be added in the correct order.

Of course this assumes that you have simple sort requirements as suggested by your example.


ArrayList q = new ArrayList(); 
foreach (object o in listBox4.Items) 
        q.Add(o);
} 
q.Sort(); 
listBox5.Items.Clear();
foreach(object o in q){
    listBox5.Items.Add(o); 
}


Try this:

var list = lstBox.Items.Cast<ListItem>().OrderBy(item => item.Text).ToList();
lstBox.Items.Clear();
foreach (ListItem listItem in list)
{
    lstBox.Items.Add(listItem);
}

If you need it to sort by the Values, just switch out item.Text with item.Value.

Enjoy!


Try the following without adding elements to any array

Listbox5.Items.AddRange(Listbox4.Items);
Listbox5.Sorted=true;


Add the items to array and close the loop. Then sort the array values and bind it to listbox


Try AddRange

    ArrayList q = new ArrayList();

    foreach (object o in listBox4.Items)
        q.Add(o);
    q.Sort();

    listBox5.Items.AddRange(q.ToArray());


If you are using .Net3.5 use linq to finish this task.Here i used list to convert and sorted

        var list = ListBox1.Items.Cast<ListItem>().Select(item => item.Value).ToList();
        list.Sort();

        ListBox2.DataSource =list;
        ListBox2.DataBind();


    private void SortListBox(ListBox listBox)
    {
        SortedList<string, string> list = new SortedList<string, string>(); 
        foreach (ListItem i in listBox.Items) {
                list.Add(i.Text, i.Value);
        } 
        listBox.Items.Clear();
        foreach(KeyValuePair<string, string> i in list){
            listBox.Items.Add(new ListItem(i.Key, i.Value)); 
        }
    }


also you can use "extension methods" that i wrote:

public static class ExtensionMethods
{
    public static void Sort(this ListControl lb, bool desc = false)
    {
        var list = lb.Items.Cast<ListItem>().ToArray();
        list = desc
                    ? list.OrderByDescending(x => x.Text).ToArray()
                    : list.OrderBy(x => x.Text).ToArray();
        lb.Items.Clear();
        lb.Items.AddRange(list);
    }
    public static void SortByValue(this ListControl lb, bool desc = false)
    {
        var list = lb.Items.Cast<ListItem>().ToArray();
        list = desc
                    ? list.OrderByDescending(x => x.Value).ToArray()
                    : list.OrderBy(x => x.Value).ToArray();
        lb.Items.Clear();
        lb.Items.AddRange(list);
    }
    public static void SortByText(this ListControl lb, bool desc = false)
    {
        lb.Sort(desc);
    }
    public static void SortRandom(this ListControl lb)
    {
        var list = lb.Items.Cast<ListItem>()
                            .OrderBy(x => Guid.NewGuid().ToString())
                            .ToArray();
        lb.Items.Clear();
        lb.Items.AddRange(list);
    }
}


Sort Listbox Desc

void sort()
    {
        if (listBox1.Items.Count <= 1)
            return;
        for (int j = 0; j < listBox1.Items.Count - 1; j++)
        {
            for (int i = 0; i < listBox1.Items.Count - 1; i++)
            {
                listBox1.SetSelected(i, true);
                string a = listBox1.SelectedItem.ToString();
                listBox1.SetSelected(++i, true);
                i--;
                string b = listBox1.SelectedItem.ToString();
                if (b.CompareTo(a) == 1)
                {
                    listBox1.Items.RemoveAt(i);
                    listBox1.Items.Insert(i, b);
                    i++;
                    listBox1.Items.RemoveAt(i);
                    listBox1.Items.Insert(i, a);
                    i--;
                }
            }
        }
    }


protected void Sort(ListBox lbox)
    {
        try
        {
            List<KeyValuePair<string, string>> ListBoxList = new 
            List<KeyValuePair<string, string>>();
            foreach (ListItem li in lbox.Items)
            {
                ListBoxList.Add(new KeyValuePair<string, string>(li.Value, li.Text));
            }
            if (ListBoxList.Count > 0)
            {
                ListBoxList = ListBoxList.OrderBy(x => x.Value).ToList();
                lbox.DataTextField = "Value";
                lbox.DataValueField = "Key";
                lbox.DataSource = ListBoxList;
                lbox.DataBind();
            }
        }
        catch (Exception error)
        {
            error.WriteEvent();
            throw;
        }
    }


For ASP.NET Listbox:

    private void SortListBox(ListBox oListBox)
    {
        ListBox oSortedListBox = new ListBox();
        oSortedListBox.DataSource = oListBox.Items.Cast<ListItem>().ToDictionary(i => i.Value, i => i.Text).OrderBy(i => i.Value);
        oSortedListBox.DataValueField = "Key";
        oSortedListBox.DataTextField = "Value";
        oSortedListBox.DataBind();

        oListBox.Items.Clear();

        foreach (ListItem oListItem in oSortedListBox.Items)
        {
            oListBox.Items.Add(oListItem);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消