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, not alphabetically. I have been search
Create an enum
and assign int
values for each month. Sort that thing with linq.
You can use a Dictionary<int,string>
instead, using the int
as a month number for sorting, then sorting by key.
IDictionary<int,string> monthList = new Dictionary<int,string>();
monthList.Add(6, "June");
monthList.Add(2, "February");
monthList.Add(8, "August");
var sorted = monthList.OrderBy(item => item.Key);