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:

[csharp]

#region Namespace References
using System;
using System.Collections;
using Microsoft.SharePoint;
#endregion

namespace buenz.WebParts
{
public class spDateComparer : IComparer
{
#region Class Constructors

public SPDateComparer()
{
}

#endregion

#region Public Methods

public virtual int Compare(object firstObject, object secondObject)
{
SPListItem firstDate = ((SPListItem)firstObject);
SPListItem secondDate = ((SPListItem)secondObject);

if (((DateTime)firstDate[“myDate1”]) < ((DateTime)secondDate["myDate2"])) { return -1; } else if (((DateTime)firstDate["myDate1"]) > ((DateTime)secondDate[“myDate2”]))
{
return 1;
}

else
{
return 0;
}
}

#endregion
}

}

[/csharp]

Share

Leave a Reply

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