code_junkie

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

Nov 12th, 2011
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. public class Thing {
  2. public string Name { get; set; }
  3. public DateTime EditDate { get; set; }
  4. }
  5.  
  6. var things = new List<Thing>();
  7.  
  8. thing.Sort((t1, t2) => t1.EditDate.CompareTo(t2.EditDate));
  9.  
  10. things.Sort((t1, t2) =>
  11. {
  12. if (t1 == null)
  13. {
  14. return (t2 == null) ? 0 : -1;
  15. }
  16. if (t2 == null)
  17. {
  18. return 1;
  19. }
  20. return t1.EditDate.CompareTo(t2.EditDate);
  21. });
  22.  
  23. TheList.Sort(int delegate(Type a, Type b)
  24. {
  25. if (a.memeber == null) return -1;
  26. if (b.memeber == null) return 1;
  27. if (a.memeber<b.memeber) return -1;
  28. if (b.memeber<a.memeber) return 1;
  29. return 0;
  30. }
Add Comment
Please, Sign In to add comment