How do you sort a generic list in C# and allow NULL items to appear first in the list?

You can create a somewhat more complex lambda, such as: things. Sort((t1, t2) => { if (t1 == null) { return (t2 == null)? 0 : -1; } if (t2 == null) { return 1; } return t1.EditDate.

CompareTo(t2. EditDate); }) EndDate cannot be null as it is a value type. However, if you had a specific value of EndDate that you consider as null you could modify the above code to take that into account in a similar fashion to how I've coped with t1 or t2 being null.

You can create a somewhat more complex lambda, such as: things. Sort((t1, t2) => { if (t1 == null) { return (t2 == null)? 0 : -1; } if (t2 == null) { return 1; } return t1.EditDate.

CompareTo(t2. EditDate); }); EndDate cannot be null as it is a value type. However, if you had a specific value of EndDate that you consider as null, you could modify the above code to take that into account in a similar fashion to how I've coped with t1 or t2 being null.

You should switch == to Object. Reference equals. You're looking for object identity but asking for object equality.

These are sadly two different items with only one construct in C# – JaredPar Nov 25 '08 at 18:28 For this example your code works. But in a more abstract sample, it's possible for someone to overload == to not be reference identity (strings for instance). In that case null can be intepreted differently and give interesting results.

– JaredPar Nov 25 '08 at 18:30 Yup, that's a fair point. – Jeff Yates Nov 26 '08 at 13:39.

Have Thing implement IComparable such that Things with null editDates have a higher order than those with an edit date. Then modify the sort expression to sort on the objects and not the edit dates.

List has a sort function that takes a delegate. Now that I've read the question TheList. Sort(int delegate(Type a, Type b) { if (a.

Memeber == null) return -1; if (b. Memeber == null) return 1; if (a. MemeberMemeber) return -1; if (b.

Memeber.

The answer from ffpf should work. But if you have some time read through this great post about sorting with nulls from the BCL team blogs.msdn.com/bclteam/archive/2008/10/0....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions