I wanted to see how well I could outperform Chris's solution with my own IComparer. The difference was negligible. To sort the same list of one million dates, my solution took 63.2 seconds, and Chris's took 66.2 seconds.
/// <summary>
/// Date strings must be in the format [M]M/[D]D/YYYY
/// </summary>
class DateStringComparer : IComparer<string>
{
private static char[] slash = { '/' };
public int Compare(string Date1, string Date2)
{
// get date component strings
string[] strings1 = Date1.Split(slash);
string[] strings2 = Date2.Split(slash);
// get date component numbers
int[] values1 = { Convert.ToInt32(strings1[0]),
Convert.ToInt32(strings1[1]),
Convert.ToInt32(strings1[2]) };
int[] values2 = { Convert.ToInt32(strings2[0]),
Convert.ToInt32(strings2[1]),
Convert.ToInt32(strings2[2]) };
// compare year, month, day
if (values1[2] == values2[2])
if (values1[0] == values2[0])
return values1[1].CompareTo(values2[1]);
else
return values1[0].CompareTo(values2[0]);
else
return values1[2].CompareTo(values2[2]);
}
}
As for sorting the dates as pre-existing DateTime instances, that took 252 milliseconds.