C# lambda expressions and IComparer
Asked Answered
D

4

22

I am using lambda expressions to sort and search an array in C#. I don't want to implement the IComparer interface in my class, because I need to sort and search on multiple member fields.

class Widget
{
    public int foo;

    public void Bar()
    {
        Widget[] widgets;

        Array.Sort(widgets, (a, b) => a.foo.CompareTo(b.foo));

        Widget x = new Widget();
        x.foo = 5;
        int index = Array.BinarySearch(widgets, x,
                                       (a, b) => a.foo.CompareTo(b.foo));
    }
}

While the sort works fine, the binary search gives a compilation error Cannot convert lambda expression to type 'System.Collections.IComparer<Widget>' because it is not a delegate type. For some reason, Sort has overloads for both IComparer and Comparison, but BinarySearch only supports IComparer. After some research, I discovered the clunky ComparisonComparer<T> to convert the Comparison to an IComparer:

public class ComparisonComparer<T> : IComparer<T>
{
    private readonly Comparison<T> comparison;

    public ComparisonComparer(Comparison<T> comparison)
    {
        this.comparison = comparison;
    }

    int IComparer<T>.Compare(T x, T y)
    {
        return comparison(x, y);
    }
}

This allows the binary search to work as follows:

int index = Array.BinarySearch(
  widgets,
  x,
  new ComparisonComparer<Widget>((a, b) => a.foo.CompareTo(b.foo)));

Yuck. Is there a cleaner way?

Dylan answered 2/2, 2011 at 2:25 Comment(1)
The upcoming .NET4.5 has a method Comparer<>.Create for constructing an IComparer<> instance from an IComparison<> delegate.Finnic
E
11

Well, one option is to create something like ProjectionComparer instead. I've got a version of that in MiscUtil - it basically creates an IComparer<T> from a projection.

So your example would be:

int index = Array.BinarySearch(widgets, x,
                               ProjectionComparer<Widget>.Create(x => x.foo));

Or you could implement your own extension methods on T[] to do the same sort of thing:

public static int BinarySearchBy<TSource, TKey>(
    this TSource[] array,
    TSource value,
    Func<TSource, TKey> keySelector)
{
    return Array.BinarySearch(array, value,
                              ProjectionComparer.Create(array, keySelector));
}
Epaulet answered 2/2, 2011 at 2:37 Comment(1)
For the record, ProjectionComparer<T>, ValueComparer<T>, and ComparisonEx all look good. At the end of the day, though, I wonder why some built-in methods accept Comparison and IComparer, and others just IComparer...Dylan
P
10

You can use my ValueComparer<T> class:

int index = Array.BinarySearch(
    widgets, x,
    new ValueComparer<Widget>(x => x.Foo)
);

You can compare by multiple properties by passing multiple lambda expressions.

Phanerozoic answered 2/2, 2011 at 2:42 Comment(0)
O
3

Try this:

public static class ComparisonEx
{
    public static IComparer<T> AsComparer<T>(this Comparison<T> @this)
    {
        if (@this == null)
            throw new System.ArgumentNullException("Comparison<T> @this");
        return new ComparisonComparer<T>(@this);
    }

    public static IComparer<T> AsComparer<T>(this Func<T, T, int> @this)
    {
        if (@this == null)
            throw new System.ArgumentNullException("Func<T, T, int> @this");
        return new ComparisonComparer<T>((x, y) => @this(x, y));
    }

    private class ComparisonComparer<T> : IComparer<T>
    {
        public ComparisonComparer(Comparison<T> comparison)
        {
            if (comparison == null)
                throw new System.ArgumentNullException("comparison");
            this.Comparison = comparison;
        }

        public int Compare(T x, T y)
        {
            return this.Comparison(x, y);
        }

        public Comparison<T> Comparison { get; private set; }
    }
}

It lets you use this code:

Comparison<int> c = (x, y) => x == y ? 0 : (x <= y ? -1 : 1);
IComparer<int> icc = c.AsComparer();

Func<int, int, int> f = (x, y) => x == y ? 0 : (x <= y ? -1 : 1); 
IComparer<int> icf = f.AsComparer();
Odie answered 2/2, 2011 at 3:7 Comment(1)
ArgumentNullException takes a parameter name, without a type.Phanerozoic
P
1

if you want to avoid using external libraries, another option for you would be to first filter out the array and object. e.g.

int index = Array.BinarySearch(
  widgets.Select(x=>x.foo).ToArray(),
  x.foo)
Peele answered 20/12, 2020 at 18:55 Comment(1)
Do you know if C#'s Array.BinarySearch still doesn't support Lambda expressions in 2024? It is made worse by the fact that I could not find a C# feature to support other means of implementing interfaces anonymously.Brittney

© 2022 - 2024 — McMap. All rights reserved.