我有一个字符串列表,其中每个字符串都是月份和年份
List<string> Dates = new List<string>
{
"DEC16",
"SEP16",
"JUN16",
"MAR15"
};
任何方式来订购它
"MAR15",
"JUN16",
"SEP16",
"DEC16"
答案 0 :(得分:5)
你可以:
List<string> Dates = new List<string> {
"DEC16",
"SEP16",
"JUN16",
"MAR15"
};
Dates.Sort((p, q) =>
DateTime.ParseExact(p, "MMMyy", CultureInfo.InvariantCulture).CompareTo(
DateTime.ParseExact(q, "MMMyy", CultureInfo.InvariantCulture)));
这是因为使用DateTime.ParseExact
您可以实际解析这些日期(它们将转换为1 December 2016 00:00
,依此类推)
请注意,月份必须是JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC(大写或小写)
答案 1 :(得分:1)
var sortedMonths = Dates
.Select(x => new {
Name = x,
Sort = DateTime.ParseExact(x, "MMMyy", CultureInfo.InvariantCulture)
})
.OrderBy(x => x.Sort)
.Select(x => x.Name)
.ToArray();