Wednesday, September 02, 2009 3:47 PM
I needed to do a quick sort on a DropDownList Server Control and found a great way of doing it in just 4 lines of code. Your project will need to be pointed to the .NET 3.5 Framework and will need a reference to System.Linq.
List<string> list = DropDownList.Items.Cast<ListItem>().Select(item => item.Text).ToList();
list.Sort((x, y) => string.Compare(x, y));
DropDownList.DataSource = list;
DropDownList.DataBind();