Using IComparer For SharePoint Date Comparison

Ok, so I had this requirement come up from a friend, he wanted some help writing a virtual method that would allow him to compare to SharePoint dates. I am curious if there is a better way to write this, because what I ended up with was disappointing to me:

  1. #region Namespace References
  2. using System;
  3. using System.Collections;
  4. using Microsoft.SharePoint;
  5. #endregion
  6.  
  7. namespace buenz.WebParts
  8. {
  9. public class spDateComparer : IComparer
  10. {
  11. #region Class Constructors
  12.  
  13. public SPDateComparer()
  14. {
  15. }
  16.  
  17. #endregion
  18.  
  19. #region Public Methods
  20.  
  21. public virtual int Compare(object firstObject, object secondObject)
  22. {
  23. SPListItem firstDate = ((SPListItem)firstObject);
  24. SPListItem secondDate = ((SPListItem)secondObject);
  25.  
  26. if (((DateTime)firstDate["myDate1"]) < ((DateTime)secondDate["myDate2"]))
  27. {
  28. return -1;
  29. }
  30.  
  31. else if (((DateTime)firstDate["myDate1"]) > ((DateTime)secondDate["myDate2"]))
  32. {
  33. return 1;
  34. }
  35.  
  36. else
  37. {
  38. return 0;
  39. }
  40. }
  41.  
  42. #endregion
  43. }
  44.  
  45. }
share save 171 16 Using IComparer For SharePoint Date Comparison

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>