I have a generic list of objects in C#, for example sake, here’s what the object might be.
public class Thing { public string Name { get; set; } public DateTime EditDate { get; set; } } var things = new List<Thing>();
Now I want to call:
thing.Sort((t1, t2) => t1.EditDate.CompareTo(t2.EditDate));
However, some of my EditDate properties are null. I want these objects with a null EditDate property to show up at the top of my list (i.e. closer to the zero index). How can I do this?
You can create a somewhat more complex lambda, such as:
EndDatecannot benullas it is a value type. However, if you had a specific value ofEndDatethat you consider asnull, you could modify the above code to take that into account in a similar fashion to how I’ve coped witht1ort2beingnull.