Sorting a list of items in a list box

2019-01-14 18:28发布

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.Add(q.ToString());

But it doesnt work. Any ideas?

10条回答
霸刀☆藐视天下
2楼-- · 2019-01-14 19:04

Try AddRange

    ArrayList q = new ArrayList();

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

    listBox5.Items.AddRange(q.ToArray());
查看更多
一夜七次
3楼-- · 2019-01-14 19:08
    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)); 
        }
    }
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-14 19:10

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--;
                }
            }
        }
    }
查看更多
Rolldiameter
5楼-- · 2019-01-14 19:13

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.

查看更多
【Aperson】
6楼-- · 2019-01-14 19:15

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();
查看更多
老娘就宠你
7楼-- · 2019-01-14 19:16
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;
        }
    }
查看更多
登录 后发表回答