LuckyDan Posted October 15, 2003 Posted October 15, 2003 I have 2 listboxes the first one is populated by a Oracledatabase and I sorted it by my sql query(order by) the second must be populated by selecting items from the first one that's no problem but I want that the second listbox should be sorted also and when i retrieve items from the second i want to put them sorted in the first one do i have to use an array? or are there other means? Quote
*Experts* Volte Posted October 15, 2003 *Experts* Posted October 15, 2003 You could set the Sorted property to true if you just want simple alphabetic order. If you want a different order, you will need to write a class which derives from IComparer and use it with the Sort method of af an array. An IComparer is just an object which compares two objects and returns whether [Thing A] is less than, equal to, or greater than [Thing B] so it knows where to put it in the array. Note that if you want to use a different IComparer, you will need to use an array to sort it and then add to the listbox. Quote
LuckyDan Posted October 23, 2003 Author Posted October 23, 2003 Thanks but I use a webform and not a windowform Quote
Administrators PlausiblyDamp Posted October 23, 2003 Administrators Posted October 23, 2003 Probably best going with VolteFace's suggestion of putting it in an array and sorting that. As an aside - if you post a question in the Windows Forms forum people are going to assume the question is about Windows Forms. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
LuckyDan Posted October 23, 2003 Author Posted October 23, 2003 thanks it's was my first time I posted a question have a nice day Quote
mocella Posted October 23, 2003 Posted October 23, 2003 (edited) For a web-form you can do this: //GENERIC FUNCTION FOR SORTING A LIST BOX private void SortListBox(ListBox currList) { //COPY THE MEMBERS OF THE LISTBOX INTO AN ARRAY LIST ArrayList arraylist = new ArrayList(); foreach(ListItem li in currList.Items) { arraylist.Add(li); } arraylist.Sort(new ListObject(ListObject.Directions.ascending)); //SORT THE LIST - ASCENDING //REMOVE THE EXISTING ITEMS FROM LISTBOX currList.Items.Clear(); //RE-ADD THE SORTED ITEMS TO THE LISTBOX for(int i = 0 ; i < arraylist.Count ; i++) { currList.Items.Add((ListItem) arraylist[i]); } currList.SelectedIndex = -1; //ENSURE NO SELECTED ITEM } //*** NEED THIS FOR ARRAY-LIST SORTING: *** private class ListObject : IComparer { public enum Directions : int { ascending = 1, descending = (-1) }; private int direction = (int) ListObject.Directions.ascending; public ListObject() {} public ListObject(Directions direction) { this.direction = (int) direction; } public int Compare(object x, object y) { string xstring = ((ListItem) x).Text; string ystring = ((ListItem) y).Text; return xstring.CompareTo(ystring) * this.direction; } } //*******************************************[ Edited October 23, 2003 by Volte Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.