In c# any type that implements comparison operators like <
>
, can easily be compared. For example I can do this:
var date1 = new DateTime(1000);
var date2 = new DateTime(2000);
var date3 = new DateTime(3000);
var result = date1 < date2; // true
However, i'm not able to do the following
var result = date1 < date2 < date3; // error
this doesn't compile, since the first comparison returns a boolean, which isn't further compareable to other dates
So I have to do it like this instead (DateTime.CompareTo(DateTime)
returns -1 if first DateTime
is earlier:
var result = date1.CompareTo(date2) + date2.CompareTo(date3) == -2; // true
Or simply do this:
var result = date1 < date2 && date2 < date3; // true
However, I was wondering if there was some possibility to chain the <
Operator multiple times, for the ease of writing some easier to read code, when this is used in more complicated scenarios.
For example I need to do this (which of course doen't compile):
result =
date1 < date2 < date3 < date4 ||
date3 < date4 < date1 < date2 ||
date4 < date1 < date2 < date3 ||
date2 < date3 < date4 < date1
which would lead to much easier readable code than the above presented possibilities which work.
Is there an easy way to do this, do I need to implement it myself?
Here's what I'd do:
public static class Extensions
{
public static bool InOrderAscending<T>(this IEnumerable<T> values)
where T : struct, IComparable
=>
!values.Zip(values.Skip(1), (value, nextValue) => value.CompareTo(nextValue))
.Any(x => x >= 0);
public static bool InOrderAscending<T>(params T[] values) where T : struct, IComparable
=> values.InOrderAscending();
}
Here's how that works: Zip()
takes two IEnumerables and enumerates the items in
them as matched pairs:
var a = new[] { 1, 2, 3 };
var b = new[] { 4, 5, 6, 7 };
var zipped = a.Zip(b, (aitem, bitem) => $"{aitem},{bitem}").ToList();
zipped will contain { "1, 4", "2, 5", "3, 6" }
.
Note that 7
is unused: There's no match so it's discarded. This is in accordance with the LINQ philosophy of never having to do range-checking.
Next, Skip(1)
skips one item and enumerates the rest.
So what I'm doing is zipping two sequences: The original one, and the second-through-final items of the original one.
{a, b, c}
{b, c}
So that'll give us a sequence of (a, b) and (b, c).
This is less readable than comparing arg[i]
to arg[i+1]
, but it spares you dealing with indexes.
So our zip expression returns a sequence of comparison results. For each adjacent pair of items, we call CompareTo() and return the result.
public static bool InOrderDescending<T>(params T[] values) where T : struct, IComparable
{
List<int> comparisons =
values.Zip(values.Skip(1), (value, nextValue) => value.CompareTo(nextValue))
.ToList();
// Now we finish by checking that sequence of integers for any positive values,
// where a positive value means that `value` was greater than `nextValue`
var haveOutOfOrderItems = comparisons.Any(x => x >= 0);
// If none of the values were positive, we're in order.
return !haveOutOfOrderItems;
}
I've written this method for value types only, so I don't have to worry about nulls. Is null
greater or lesser than new Button()
or this.SettingsPage
? That's up to the caller, so I'd write a reference-type overload that takes a parameter of type IComparer<T>
, or just a lambda (Edit: Perhaps we should actually write an extension method that does the self-offset-zip, but returns a sequence of some arbitrary return type from the lambda; we’d use that to write this).
public static bool InOrderAscending<T>(this IEnumerable<T> values, Func<T, T, int> compare)
where T : class
=>
!values.Zip(values.Skip(1), (value, nextValue) => compare(value, nextValue))
.Any(x => x >= 0);