How to sort the DropDownList

I have the following code that populates the DropDownList:

DataSet ds = new DataSet();
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();
SPList list = myWeb.Lists["GuidelineTopics"];
DTable_List = list.Items.GetDataTable();
DTable_List.TableName = "Table1";
DTable_List.DefaultView.Sort = "Title ASC";
ds.Tables.Add(DTable_List);
Topic.DataSource = ds.Tables["Table1"];
Topic.DataSource = DTable_List;
Topic.DataTextField = "Title";
Topic.DataValueField = "Title";
Topic.DataBind();
Topic.Items.Insert(0, new ListItem("All Topics", "All Topics"));
Topic.SelectedIndex = 0;

      

How can I apply SORT to a list so that it is either ASC or DESC in alphabetical order?

+3


source to share


1 answer


Try the following. You can use Linq OrderBy to get what you want.

If you want to sort the data source in ascending order of the column value, do

Topic.DataSource = ds.Tables["Table1"].OrderBy(x => x.Title);

      



Or, if you want to sort descending by a specific column name, do

Topic.DataSource = ds.Tables["Table1"].OrderByDescending(x => x.Title);

      

+5


source