13

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?

Codie Vincent
  • 187
  • 2
  • 5
  • 12
  • 7
    Any reason you do not just set the `ListBox.Sorted` property to true on the second ListBox, then just add the items to the LsitBox and the ListBox will take care of the sorting. Unless of course your sort criteria is more complex than your example suggests. – Chris Taylor Sep 08 '10 at 11:20

12 Answers12

29

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.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
14
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); 
}
HCL
  • 36,053
  • 27
  • 163
  • 213
11

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!

Trey Gramann
  • 2,004
  • 20
  • 23
2

Try the following without adding elements to any array

Listbox5.Items.AddRange(Listbox4.Items);
Listbox5.Sorted=true;
Herbert
  • 75
  • 8
1

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

Bala
  • 1,386
  • 1
  • 14
  • 27
1

Try AddRange

    ArrayList q = new ArrayList();

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

    listBox5.Items.AddRange(q.ToArray());
Branimir
  • 4,327
  • 1
  • 21
  • 33
0
    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)); 
        }
    }
0

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);
    }
}
Devrim Altınkurt
  • 2,887
  • 1
  • 10
  • 7
0

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();
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
0

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--;
                }
            }
        }
    }
help-info.de
  • 6,695
  • 16
  • 39
  • 41
0
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;
        }
    }
Stewart
  • 51
  • 5
0

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);
        }
    }
Msxmania
  • 53
  • 2
  • 9