我之前在这里问过这个问题:List of Dates ordered in a certain way
我认为提议的解决方案很好,直到我的日期列表上的那一年结束并遇到问题。
我的日期列表(以这种基于字符串的格式 - 这是数据从源 API 传给我的方式)
201711
201712
201801
201811
201812
201901
我想在条形图中显示我的数据,以显示3 个月按月顺序进行的同比比较。这意味着我按原样排序列表
201711
201811
201712
201812
201801
201901
这样我就可以按顺序查看 11 月、12 月和 1 月的同比条形图。
我已经尝试了问题底部的解决方案,但它的顺序是这样的(这不是我想要的):
201801
201901
201711
201811
201712
201812
为了清楚起见,下个月需要向前移动到这个日期列表:
我想要的第一个月总是比当前月份早 2 个月
201712
201812
201801
201901
201802
201902
var rowsInOrder = dateList.OrderBy(n => DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Month).ThenBy(n=> DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Year);
最佳答案
您可以使用这种首先确定月份组的查找方法:
var monthLookup = dateList
.Select(s => new{String = s, Date = DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture)})
.OrderBy(x => x.Date) // not necessary in your sample data but i assume it's desired
.ToLookup(x=> x.Date.Month);
var rowsInOrder = monthLookup.SelectMany(x => x).Select(x => x.String);
关于c# - 用于比较的订单日期列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513984/