@DavidMillsによって受け入れられた答えがありますかなり良いですが、私はそれが改善できると思います。 1つは、フレームワークにすでに静的メソッドComparer<T>.Create(Comparison<T>)
が含まれている場合に、ComparisonComparer<T>
クラスを定義する必要はありません。この方法は、即時にIComparison
を作成するために使用できます。
また、危険である可能性のあるIList<T>
からIList
にキャストします。私が見たほとんどの場合、を実装するために、IList
を実装するList<T>
が使用されていますが、これは保証されておらず、脆弱なコードにつながる可能性があります。
最後に、オーバーロードされたList<T>.Sort()
メソッドは4つのシグネチャを持ち、そのうち2つだけが実装されています。
public static class IListExtensions
{
public static void Sort<T>(this IList<T> list)
{
if (list is List<T>)
{
((List<T>)list).Sort();
}
else
{
List<T> copy = new List<T>(list);
copy.Sort();
Copy(copy, 0, list, 0, list.Count);
}
}
public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
{
if (list is List<T>)
{
((List<T>)list).Sort(comparison);
}
else
{
List<T> copy = new List<T>(list);
copy.Sort(comparison);
Copy(copy, 0, list, 0, list.Count);
}
}
public static void Sort<T>(this IList<T> list, IComparer<T> comparer)
{
if (list is List<T>)
{
((List<T>)list).Sort(comparer);
}
else
{
List<T> copy = new List<T>(list);
copy.Sort(comparer);
Copy(copy, 0, list, 0, list.Count);
}
}
public static void Sort<T>(this IList<T> list, int index, int count,
IComparer<T> comparer)
{
if (list is List<T>)
{
((List<T>)list).Sort(index, count, comparer);
}
else
{
List<T> range = new List<T>(count);
for (int i = 0; i < count; i++)
{
range.Add(list[index + i]);
}
range.Sort(comparer);
Copy(range, 0, list, index, count);
}
}
private static void Copy(IList<T> sourceList, int sourceIndex,
IList<T> destinationList, int destinationIndex, int count)
{
for (int i = 0; i < count; i++)
{
destinationList[destinationIndex + i] = sourceList[sourceIndex + i];
}
}
}
:
List<T>.Sort()
List<T>.Sort(Comparison<T>)
List<T>.Sort(IComparer<T>)
- は
List<T>.Sort(Int32, Int32, IComparer<T>)
以下のクラスは、IList<T>
インターフェイスのすべての4 List<T>.Sort()
署名を実装します
使用法:
class Foo
{
public int Bar;
public Foo(int bar) { this.Bar = bar; }
}
void TestSort()
{
IList<int> ints = new List<int>() { 1, 4, 5, 3, 2 };
IList<Foo> foos = new List<Foo>()
{
new Foo(1),
new Foo(4),
new Foo(5),
new Foo(3),
new Foo(2),
};
ints.Sort();
foos.Sort((x, y) => Comparer<int>.Default.Compare(x.Bar, y.Bar));
}
ここでの考え方は、可能な限り、ソート処理するための基礎となるList<T>
の機能を利用することです。繰り返しになりますが、私が見た実装の多くはIList<T>
です。基になるコレクションが異なるタイプの場合、入力リストの要素を持つList<T>
の新しいインスタンスを作成することに代えて、それを使用してソートを行い、結果を入力リストにコピーし直します。これは入力リストがIList
インターフェイスを実装していなくても機能します。
最初にIListを返すのはなぜですか? WCFサービスからですか? – DaeMoohn