Reputation: 196459
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:
I want this:
Upvotes: 1
Views: 138
Reputation: 126804
You should be able to utilize the null coalescing operator. For example:
var orderedDates = nullableDates.OrderBy(date => date ?? DateTime.MaxValue);
This will subsitute the maximum value for any missing (null) dates, pushing those to the end of the sorted sequence. If this date is presumably part of an object, you can of course use this to order a sequence by a date property of the object.
var orderedFoos = foos.OrderBy(foo => foo.SomeDate ?? DateTime.MaxValue);
Upvotes: 10