There are multiple ways you can solve this problem.
Imperative solution
1: public static class DateTimeExtensions
2: { 3: public static bool AreChronological(this IEnumerable<DateTime?> dateTimes)
4: { 5: var prev = (DateTime?)DateTime.MinValue; 6: 7: foreach (var dateTime in dateTimes)
8: {9: if (dateTime != null)
10: {11: if (prev > dateTime)
12: {13: return false;
14: } 15: prev = dateTime; 16: } 17: } 18: 19: return true;
20: } 21: }After I finished the imperative solution, I asked Bart De Smet if he knew a sexier solution. He completely Linqified the solution and came up with this.
1: public static bool AreChronological(this IEnumerable<DateTime?> dateTimes)
2: {3: return dateTimes.Where(d => d != null)
4: .Aggregate(new { c = (DateTime?)DateTime.MinValue, b = true },
5: (a, d) => new { c = d, b = a.b && a.c <= d }).b;
6: }Usage
You can use the extension method like this.
1: DateTime?[] dateTimesChronological = new DateTime?[] { new DateTime(2010, 5, 15),
2: null,
3: new DateTime(2010, 6, 20) };
4: DateTime?[] dateTimesNotChronological = new DateTime?[] { new DateTime(2010, 6, 20),
5: null,
6: new DateTime(2010, 5, 15) };
7: 8: Console.WriteLine(dateTimesChronological.AreChronological()); 9: Console.WriteLine(dateTimesNotChronological.AreChronological());Your opinion
Which solution do you like best? The imperative or the functional solution? Any ideas on how I could improve this code?