Sorting months in a list

后端 未结 8 2019
闹比i
闹比i 2020-12-19 10:00

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

相关标签:
8条回答
  • 2020-12-19 10:44

    Create an enum and assign int values for each month. Sort that thing with linq.

    0 讨论(0)
  • 2020-12-19 10:45

    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);
    
    0 讨论(0)
提交回复
热议问题