C# 根据日历年对月份进行排序

C# 根据日历年对月份进行排序,c#,algorithm,sorting,C#,Algorithm,Sorting,我目前有一些代码可以按字母顺序对字符串排序。我想让它根据日历年对月份进行排序。我已经在这个网站上看到了一些方法,但无法将它们具体应用到我的代码中。我需要添加什么才能使其按顺序排序(1月、2月等) 要将月份读取到数组的代码: var month1Values = File .ReadAllLines(monthFilePath) .Select(x => new { Name = monthFilePath, Sort = DateTime.ParseExact(x, "MM

我目前有一些代码可以按字母顺序对字符串排序。我想让它根据日历年对月份进行排序。我已经在这个网站上看到了一些方法,但无法将它们具体应用到我的代码中。我需要添加什么才能使其按顺序排序(1月、2月等)

要将月份读取到数组的代码:

var month1Values = File
    .ReadAllLines(monthFilePath)
    .Select(x => new { Name = monthFilePath, Sort = DateTime.ParseExact(x, "MMMM", CultureInfo.InvariantCulture) })
    .ToArray(); 
要排序的代码:

if (SortFile == 3)
{                 
    comparison1 = string.Compare(fileData[index].MonthValues, 
        fileData[index + 1].MonthValues) > 0;

    if (comparison1)
    {
        temp = fileData[index].MonthValues;
        fileData[index].MonthValues = fileData[index + 1].MonthValues;
        fileData[index + 1].MonthValues = temp;
        swap = true;
     }
 } 
在asterix为的位置出现错误:

for (var index = 0; index < datasize; index++)
            {
                fileData[index] = new FileData
                {
            DayValues = day1Values[index],
            MonthValues = *month1Values[index]*,

                 };
            }
for(var index=0;index

错误读取无法将类型“”转换为“string”。如何解决此问题?

您正在
Select
指令(第一个代码框,第3行)中创建匿名数据类型,而不是依赖
DateTime
类型。运行库不知道如何将匿名类型强制转换为
字符串

这应该如预期的那样起作用:

.Select(x => DateTime.ParseExact(x, "MMMM", CultureInfo.InvariantCulture)) .Select(x=>DateTime.ParseExact(x,“MMMM”,CultureInfo.InvariantCulture))
请注意,正常的
DateTime
->
String
cast需要从给定的月份创建完整的日期;这涉及到假设一天和一年是当前的,小时、分钟和秒都归零。

我在这个网站上看到了实现这一点的方法
-你能给我们展示一个使用这些方法之一但不起作用的方法吗?但这在列表中,为什么您认为它在数组中的工作方式会有所不同?如果必须,请尝试调用
.ToList()
查看
ReadAllLines
的结果,您将有一个列表可供使用。但我仍然不知道实际要做什么??我对c#还是比较陌生的。你能更详细地解释一下吗?然后把你收到的具体错误信息作为一篇文章发表。