【问题标题】:Sorting dates from a string array从字符串数组中排序日期
【发布时间】:2015-06-03 23:36:47
【问题描述】:

如果我在字符串数组中有日期,例如 23/05/2011、17/03/2012、01/07/2010 等,是否可以先对年进行排序,然后对月进行排序,然后对天进行排序,而不是对天进行排序第一个?

【问题讨论】:

  • 如果它们在字符串数组中,它们是字符串(不是日期),因为字符串数组保存...嗯,你知道
  • 是的,但首先你应该问自己,字符串数组是否应该是字符串数组而不是 DateTime 数组。

标签: c# arrays string date


【解决方案1】:

您可以先将它们解析为DateTime:

var orderedByDateTime = strings
    .Select(s => new { s, dt = DateTime.ParseExact(s, "dd'/'MM'/'yyyy", null) })
    .OrderBy(x => x.dt)
    .Select(x => x.s);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2021-10-25
    • 2015-03-15
    • 2017-04-24
    • 2020-12-14
    • 2019-11-07
    • 1970-01-01
    相关资源
    最近更新 更多