Okay.. that means there isn't one. I just spent some time to write one.
Hope this can help other people. Assuming you have the ListBox defined as
"uiProductListBox" in your aspx page, here is how to use it: (Note: you
may have to define CurrentCulture in your global.asax)
How to use?
--------------------------------------------------------------
Listbox.Sort(ref uiProductListBox, Listbox.SortOrder.Descending);
--------------------------------------------------------------
public class Listbox
{
public enum SortOrder
{
Ascending, Descending
};
private class Reverser : IComparer
{
// You can use CaseInsensitiveComparer.Compare if you want Non-case
sensitive comparison
int IComparer.Compare( Object x, Object y )
{
return( (new Comparer(Thread.CurrentThread.CurrentCulture)).Compare( y,
x ) );
}
}
public static void Sort(ref System.Web.UI.WebControls.ListBox myListBox,
SortOrder mySortOrder)
{
SortedList myList;
IComparer myComparer = new Reverser();
if (mySortOrder == SortOrder.Ascending)
myList = new SortedList();
else
myList = new SortedList(myComparer);
for (int i=0; i< myListBox.Items.Count; i++)
myList.Add(myListBox.Items
.Text.Trim(),
myListBox.Items.Value.Trim());
myListBox.Items.Clear();
for (int i=0; i< myList.Count; i++)
myListBox.Items.Add(new
System.Web.UI.WebControls.ListItem(myList.GetKey(i).ToString().Trim(),
myList.GetByIndex(i).ToString().Trim()));
}
}
C.P.
00_ChInkPoIntD12 said:
may not work in my case, because I let customers to add their own item to
the Listbox and also picking existing pre-defined items. How would you
suggest a good way to handle this? Thanks in advance..