If you want a clean distinction of the selector and comparer, you can use this helper class. It's too bad you can't have a "class extension method" to give Array.Sort
a set of overloads that take a selector.
public static class SelectingComparer<T>
{
public static IComparer<T> Create<U>(Func<T, U> selector)
{
return new SelectingComparerImpl<U>(selector, null);
}
public static IComparer<T> Create<U>(Func<T, U> selector, IComparer<U> comparer)
{
return new SelectingComparerImpl<U>(selector, comparer.Compare);
}
public static IComparer<T> Create<U>(Func<T, U> selector, Comparison<U> comparison)
{
return new SelectingComparerImpl<U>(selector, comparison);
}
private class SelectingComparerImpl<U>
: IComparer<T>
{
private Func<T, U> selector;
private Comparison<U> comparer;
public SelectingComparerImpl(Func<T, U> selector, Comparison<U> comparer)
{
if (selector == null)
throw new ArgumentNullException();
this.selector = selector;
this.comparer = comparer ?? Comparer<U>.Default.Compare;
}
public int Compare(T x, T y)
{
return this.comparer(this.selector(x), this.selector(y));
}
}
}
In use:
public class Testing
{
public void Foo()
{
FileInfo[] files = new FileInfo[30];
// FILL THE FILE ARRAY
Array.Sort(files, SelectingComparer<FileInfo>.Create(file => file.Name));
Array.Sort(files, SelectingComparer<FileInfo>.Create(file => file.Name, StringComparer.OrdinalIgnoreCase));
}
}