...ing logging 4.0

はてなブログに移行しました。D言語の話とかいろいろ。

なんでC#ネゲータ自作せなあかんのん?

public class Negater<T> : System.Collections.Generic.IComparer<T>
{
    private IComparer<T> comparer;
    public Negater(IComparer<T> comparer)
    {
        this.comparer = comparer;
    }
    public int Compare(T x, T y)
    {
        return this.comparer.Compare(y, x); // 反転する
    }
}
List<MyClass> list = new List<MyClass>();
list.Sort(new Negater<MyClass>(new MyClassComparer()));

あれー,これネゲータじゃないか? 論理値を反転するのがネゲータだったかも.