1

I have an array of DateTime? objects and I want to sort them to render them on a webpage. The issue is that the ones with no value seem to show up at the top of the list and I want them to show up at the bottom.

What is the best way to sort an array of DateTime? that will have the items with no value set show up at the bottom.

So instead of this:

  1. [No Value]
  2. Jan 1, 2013
  3. Jan 15, 2013

I want this:

  1. Jan 1, 2013
  2. Jan 15, 2013
  3. [No Value]
4

2 回答 2

10

您应该能够使用空合并运算符。例如:

var orderedDates = nullableDates.OrderBy(date => date ?? DateTime.MaxValue);

这将替换任何缺失(空)日期的最大值,将它们推到排序序列的末尾。如果此日期可能是对象的一部分,您当然可以使用它来按对象的日期属性对序列进行排序。

var orderedFoos = foos.OrderBy(foo => foo.SomeDate ?? DateTime.MaxValue);
于 2013-05-28T02:27:37.283 回答
0
var ordered = dates.OrderBy(x => x ?? DateTime.MaxValue);
于 2013-05-28T02:28:18.270 回答