Sorting months in a list

后端 未结 8 2050
闹比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:45

    You can use a Dictionary instead, using the int as a month number for sorting, then sorting by key.

    IDictionary monthList = new Dictionary();
    monthList.Add(6, "June");
    monthList.Add(2, "February");
    monthList.Add(8, "August");
    
    var sorted = monthList.OrderBy(item => item.Key);
    

提交回复
热议问题