This is similar to @Jeffrey's answer, but instead of parsing each date, it first finds the min and max dates comparing its string values, and then it parses the values at the end.
// This method handles the date comparisons
private int WeirdComparer(string strDate1, string strDate2)
{
int res = string.Compare(strDate1, 6, strDate2, 6, 4);
if (res == 0)
res = string.Compare(strDate1, 3, strDate2, 3, 2);
if (res == 0)
res = string.Compare(strDate1, 0, strDate2, 0, 2);
return res;
}
public void FindMinAndMaxDates(IList<string> strDates, out DateTime minDate, out DateTime maxDate)
{
string min = "99.99.9999";
string max = "00.00.0000";
foreach (string strDate in strDates)
{
if (WeirdComparer(strDate, min) < 0)
min = strDate;
if (WeirdComparer(strDate, max) > 0)
max = strDate;
}
minDate = DateTime.ParseExact(min, "dd.MM.yyyy", null);
maxDate = DateTime.ParseExact(max, "dd.MM.yyyy", null);
}
O(n)
time unless you put it into some type of heap structure that would let you solve it inO(logN)
time but then you'd trade processor cycles which are amazingly cheap for double the memory storage which is less cheap comparatively. – Ammon