Is there an easy way to stack Comparison operators in c#?

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);

as @Ed Plunkett and @juharr suggested, I implemented InOrder()

public static bool InOrder<T>(this IEnumerable<T> collection) where T : struct, IComparable
{
    var array = collection.ToArray();
    if (array.Length < 2)
        return true;
    var comp = 0;
    for (int i = 0; i < array.Length - 1; i++)
    {
        comp += array[i].CompareTo(array[i + 1]);
    }

    return comp == 1 - array.Length;
}

public static bool InOrder<T>(params T[] args) where T: struct, IComparable
{
    return args.InOrder();
}

It produces the following output

var date1 = new DateTime(1000);
var date2 = new DateTime(2000);
var date3 = new DateTime(3000);
var result = InOrder(date1, date2); // true
var result1 = InOrder(date2, date1); // false
var result2 = InOrder(date1, date2, date3); // true
var result3 = InOrder(date1, date3, date2); // false

Tags:

C#

Comparison