Hi all,
I need your help.
I have a list of strings which contains months of the year.
I need to be able to sort this list so the months are in order by month alphabetically.
I have been searching for awhile but I can't see to wrap my head around any of the solutions I've found.
Thanks in advance for any help or suggestion.
My code below
ddlmonth.Items.Clear();
 
for (int month = 1; month <= 12; month++)
{
    string monthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(month).ToUpper();
    ddlmonth.Items.Add(new ListItem(monthName, month.ToString()));
}
ddlmonth.Items.Insert(0, new ListItem("[== Selected ==]", ""));
 
DateTime dateTime = DateTime.Now;
List<ListItem> months = new List<ListItem>();
 
if (ddlyear.SelectedValue == "2024")
{
    for (int i = 12; i >= 6; i--)
    {
        months.Add(ddlmonth.Items[i]);
    }
}
else
{
    for (int i = 1; i < DateTime.Now.Month; i++)
    {
        months.Add(ddlmonth.Items[i]);
        months.Reverse();
    }
}
 
//Determines first item i.e.Equals Default Item.
ListItem defaultItem = ddlmonth.Items[0];
 
//Clears all items in the DropDownList.
ddlmonth.Items.Clear();
 
//Reverse the List of last two months.
months.Reverse();
 
//Inserts Default Item.
months.Insert(0, defaultItem);
 
//Assigning month to DropDownList.
ddlmonth.DataSource = months;
ddlmonth.DataTextField = "Text";
ddlmonth.DataValueField = "Value";
ddlmonth.DataBind();