0

There are 2 listboxes to exchange items. Each list item has text and value properties eg: "Branch",1 ; "Leaves",23,...etc After adding an item from listbox2 to listbox1, I would like to sort listbox1.

lb1 and lb2 are the listboxes.

protected void btnRemove_Click()
{
  if(lb2.Items.count > 0)
   {
     if(lb2.selectedindex >=0)
      {
        ListItem li = lb2.selecteditem;
        lb1.items.add(li);

        //sort
         List<ListItem> al = new List<ListItem>();
         foreach(listitem l in lb1.items)
          {
           al.Add(l);
          }
        lb1.items.clear();
        al.sort();
        lb1.Datasource=al;
        lb1.databind();
     }
   }
}

At al.sort(); it gives an error - "Failed to compare 2 elements in the array". How should I correct this.

6
  • 4
    The code you've given wouldn't even compile (C# is case-sensitive) and we don't know what lb2 and lb1 are. Additionally, is there any reason you're using ArrayList rather than List<T>? Commented Dec 25, 2013 at 11:57
  • They are the names of the listboxes. No reason. I was just trying out omerkamal's suggestion - forums.asp.net/t/1094374.aspx Commented Dec 25, 2013 at 12:02
  • That's a suggestion from 6 1/2 years ago... back when some people were still unfortunately on .NET 1.1. There's very little reason not to use generic collections now... Commented Dec 25, 2013 at 12:03
  • That article is six years old! Be very careful about the age of articles on the Internet. Commented Dec 25, 2013 at 12:03
  • @jonskeet, I tried using List, but the same error at sort. could you plz check Commented Dec 25, 2013 at 12:18

3 Answers 3

1

The problem with your existing code is that you are adding ListItem in ArrayList and to sort the ArrayList should contain objects which implements IComparer as stated in How can i sort Arraylist with a class?
You can add a custom IComparersomething like this,

public class MyListItemComparer : IComparer
{
    int IComparer.Compare(Object x, Object y)
    {
        //ListItem item1 = (ListItem)x;
        //ListItem item2 = (ListItem)y;
        return 1;//Your logic to compare and sort;
    }
}

and then you can call the sort method as

        ArrayList al = new ArrayList();
        ..... //Your other code
        IComparer myComparer = new MyListItemComparer();
        al.Sort(myComparer);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this to sort ur al array:

al.OrderBy(a => a);

1 Comment

It did not error at this line now, but did at 'Databind(); - "Listitem does not contain property formname' But I have DataTextfield="formname" Datavaluefield="formid"...same for both listboxes
0

Please make sure your array list contains the correct data and its not null. al.sort(); should work on arraylist of integers,string and other datatype if they inherit IComparable else you need to write your own comparer.

4 Comments

which namespace. I have system.collections and generics but i dont get collections...
it sure doesnt have nulls, each listitem has a string and int value
have u tried parsing items to string al.Add(lb1.items[i].toString());
yes. It did not error sort now, but did at 'Databind(); - "Listitem does not contain property formname' But I have DataTextfield="formname" Datavaluefield="formid"...same for both listboxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.