C# - IComparer - If datetime is null then should be sorted to the bottom not the top

前端 未结 3 1727
旧时难觅i
旧时难觅i 2021-02-09 08:29

I have a list of dates that I want to sort in an ascending order. However, the default comparer means that I have:

null
null
18/01/2011
23/01/2011
3条回答
  •  抹茶落季
    2021-02-09 08:50

    You could try this:

    messages.Sort((x, y) => (x.CreatedOn ?? DateTime.MaxValue).CompareTo(y.CreatedOn ?? DateTime.MaxValue));
    

提交回复
热议问题