Sorting a list of items in a list box

2019-01-14 18:57发布

问题:

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?

回答1:

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


回答2:

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.



回答3:

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!



回答4:

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



回答5:

Try AddRange

    ArrayList q = new ArrayList();

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

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


回答6:

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:

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


回答8:

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


回答9:

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


回答10:

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