如何在c#中对日期进行排序

时间:2022-09-25 14:03:48

I have many date in one variable like:

我在一个变量中有很多日期,如:

var date = hfAllDates.Value.Split('#');

there are four date(01/01/2016,22/01/2016,18/01/2016,05/01/2016) in date variable .

日期变量有四个日期(01/01 / 2016,22 / 01 / 2016,18 / 01 / 2016,05 / 01/2016)。

how to sort date.

如何排序日期。

Thanks

谢谢

4 个解决方案

#1


3  

add namespace using System.Linq;

使用System.Linq添加命名空间;

and use LINQ Query

并使用LINQ查询

in ascending order

按升序排列

  var orderedDateList = date.OrderBy(x => DateTime.Parse(x)).ToList();

in Descending oreder

在下降的矿工

var orderedDateList = date.OrderByDescending(x => DateTime.Parse(x)).ToList();

#2


2  

Convert date from string[] to List<DateTime> first:

首先将日期从字符串[]转换为List

List<DateTime> dtList = date.Select(x => DateTime.Parse(x)).ToList();

Then you can easily sort it:

然后你可以轻松地对它进行排序

dtList.Sort();

#3


1  

Using Linq's OrderBy:

使用Linq的OrderBy:

var sortedDates = date.OrderBy(x => DateTime.Parse(x));

var sortedDates = date.OrderBy(x => DateTime.Parse(x));

or

要么

var sortedDates = date.OrderByDescending(x => DateTime.Parse(x));

var sortedDates = date.OrderByDescending(x => DateTime.Parse(x));

if you are needed to sort dates descending

如果您需要按日期降序排序

#4


1  

I suggest using Linq:

我建议使用Linq:

var date = hfAllDates.Value.Split('#')
  .Select(line => DateTime.Parse(x))
  .OrderBy(x => x)
  .ToList();

#1


3  

add namespace using System.Linq;

使用System.Linq添加命名空间;

and use LINQ Query

并使用LINQ查询

in ascending order

按升序排列

  var orderedDateList = date.OrderBy(x => DateTime.Parse(x)).ToList();

in Descending oreder

在下降的矿工

var orderedDateList = date.OrderByDescending(x => DateTime.Parse(x)).ToList();

#2


2  

Convert date from string[] to List<DateTime> first:

首先将日期从字符串[]转换为List

List<DateTime> dtList = date.Select(x => DateTime.Parse(x)).ToList();

Then you can easily sort it:

然后你可以轻松地对它进行排序

dtList.Sort();

#3


1  

Using Linq's OrderBy:

使用Linq的OrderBy:

var sortedDates = date.OrderBy(x => DateTime.Parse(x));

var sortedDates = date.OrderBy(x => DateTime.Parse(x));

or

要么

var sortedDates = date.OrderByDescending(x => DateTime.Parse(x));

var sortedDates = date.OrderByDescending(x => DateTime.Parse(x));

if you are needed to sort dates descending

如果您需要按日期降序排序

#4


1  

I suggest using Linq:

我建议使用Linq:

var date = hfAllDates.Value.Split('#')
  .Select(line => DateTime.Parse(x))
  .OrderBy(x => x)
  .ToList();