LINQ OrderBy – using Comparer<T>.Create and F#’s Object Expressions

Yan Cui

I help clients go faster for less using serverless technologies.

.Net 4.5 introduced a handy little new method Comparer<T>.Create to aid the creation of bespoke comparers, which is great because it means that you don’t have to define a new Comparer class when it is going to be needed once.

In case you’re wondering, it’s still not possible to define anonymous implementation of interfaces in C#, but with Comparer<T>.Create you can at least create a bespoke instance of IComparer<T> from an anonymous method:

// this is the type we want to sort on
public class Occupation
{
public string Ship { get; set; }
public string Position { get; set; }
}
public class Candidate
{
public string Name { get; set; }
public Occupation Current { get; set; }
}
...
var arr =
new[]
{
new Candidate
{
Name = "Data",
Current = new Occupation { Ship = "USS Enterprise", Position = "Second Officer" }
},
new Candidate
{
Name = "James T Kirk",
Current = new Occupation { Ship = "USS Enterprise", Position = "Captain" }
},
new Candidate
{
Name = "Spock",
Current = new Occupation { Ship = "USS Enterprise", Position = "First Officer" }
}
};
var ordered = arr.OrderBy(x => x.Current, Comparer<Occupation>.Create((l, r) => l.Position.CompareTo(r.Position)));

As a side, you can also apply the same technique for other single method interfaces yourself, for instance, if you were happy to ignore the existence of the Zip extension method for the sake of the example, you could define your own Zip extension method which takes in an instance of an IZipper<T, U, V>:

// this is the interface we want to wrap
public interface IZipper<T, U, V>
{
V Zip(T x, U y) ;
}
public abstract class Zipper<T, U, V> : IZipper<T, U, V>
{
public static IZipper<T, U, V> Create(Func<T, U, V> zipper)
{
return new ZipperImpl<T, U, V>(zipper);
}
public abstract V Zip(T x, U y);
// this doesn't have to be a nested class by the way
private class ZipperImpl<X, Y, Z> : Zipper<X, Y, Z>
{
private readonly Func<X, Y, Z> _zipper;
public ZipperImpl(Func<X, Y, Z> zipper)
{
_zipper = zipper;
}
public override Z Zip(X x, Y y)
{
return _zipper(x, y);
}
}
}
...
// some extension method that uses the IZipper defined above
public static IEnumerable<V> ZipBy<T, U, V>(this IEnumerable<T> first, IEnumerable<U> second, IZipper<T, U, V> zipper)
{
// cheating here, but the point is to show the useage of Zipper<T, U, V>.Create
return first.Zip(second, zipper.Zip);
}
...
var first = Enumerable.Range(1, 5);
var second = first.Select(x => x.ToString());
first.ZipBy(second, Zipper<int, string, long>.Create((x, y) => x + long.Parse(y)));

Whilst we’re on the topic of anonymous interface implementation. F# has a nice little feature call Object Expressions, it provides a mechanism for creating anonymous types that are based on existing base type, interface, or set of interfaces, and for the two examples, here’s how that solution might look in F#:

open System.Linq
open System.Collections.Generic
// use record types instead because they're nice to read and write in F#
type Occupation = { Ship : string; Position : string }
type Candidate = { Name : string; Current : Occupation }
let arr = [| { Name = "Data"; Current = { Ship = "Enterprise"; Position = "Second Officer" } }
{ Name = "Data"; Current = { Ship = "Enterprise"; Position = "Captain" } }
{ Name = "Data"; Current = { Ship = "Enterprise"; Position = "First Officer" } } |]
let ordered = arr.OrderBy((fun c -> c.Current), { new IComparer<Occupation> with member this.Compare(x, y) = x.Position.CompareTo(y.Position) })
// define the IZipper interface
type IZipper<'T, 'U, 'V> =
abstract member Zip : 'T * 'U -> 'V
// define the ZipBy extension method
type IEnumerable<'T> with
member this.ZipBy(other : IEnumerable<'U>, zipper : IZipper<'T, 'U, 'V>) =
this.Zip(other, (fun x y -> zipper.Zip(x, y)))
let first = seq { 1..5 }
let second = first |> Seq.map string
// no need for Zipper<T, U, V> implementation at all!
first.ZipBy(second, { new IZipper<int, string, int64> with member this.Zip(x, y) = int64 x + int64 y })

Notice that there’s no need for an explicit implementation class for IZipper<T, U, V> interface at all using Object Expressions and how much less code you end up writing with F#! So seriously, why aren’t you checking out F# already!? Winking smile

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

1 thought on “LINQ OrderBy – using Comparer<T>.Create and F#’s Object Expressions”

  1. Pingback: F# Weekly #4, 2013 « Sergey Tihon's Blog

Leave a Comment

Your email address will not be published. Required fields are marked *