Linq按日期排序

chr*_*e13 0 c# linq sorting

我有一个日期字符串列表,格式如下

Apr-2016
Aug-2015
Nov-2015
Oct-2015
Sep-2015
July 2016
Run Code Online (Sandbox Code Playgroud)

码:

var sortedMonths = monthList
            .Select(x => new { month = x, Sort = DateTime.ParseExact(x, "MMM-yyyy", CultureInfo.InvariantCulture) })
            .OrderByDescending(x => x.Sort.Month)
            .Select(x => x.month)
            .ToList();
Run Code Online (Sandbox Code Playgroud)

我已经习惯了上面的陈述,但列表仍未订购.

Fel*_*ani 7

尝试按整个DateTime对象排序,而不仅仅是Month:

var sortedMonths = monthList
            .Select(x => new { month = x, Sort = DateTime.ParseExact(x, "MMM-yyyy", CultureInfo.InvariantCulture) })
            .OrderByDescending(x => x.Sort)
            .Select(x => x.month)
            .ToList();
Run Code Online (Sandbox Code Playgroud)