Company      |       Articles & Research      |      Services      |      Software      |      Contact

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:

C#:
  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

Related posts:

  1. SharePoint Security Helper Class
  2. SPView Field Comparison
  3. ToolPart Class With System.Reflection
  4. AspnetUsersinroles Partial Class
  5. AspnetPersonalizationallusers Partial Class

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment