と呼ばれるIComparable<T>.CompareTo
タイプの助けを借りてリストをソートしたいと思います。私が書いたT
Path
var shortest = new List<Path>();
//Fill shortest with elements != null
if (shortest.Contains(null))
throw new System.Exception("Path is null");
shortest.Sort();
if (shortest.Contains(null))
throw new System.Exception("Path is null");
驚いたことに、この方法
int IComparable<Path>.CompareTo(Path other)
{
if (other == null)
return -1;
if (!other.valid)
return 1;
if (pfad.Count() > other.pfad.Count())
{
return -1;
}
else if (pfad.Count() < other.pfad.Count())
{
return 1;
}
else
{
if (length > other.length)
return -1;
else
return 1;
}
}
クラスから
public class Path : IComparable<Path>
から呼び出されSort()
ますother==null
。さらに驚いたことに、最初のコードブロックで、2番目の例外がスローさshortest
れます。これは、並べ替えの前ではなく、並べ替えの後にnull値が含まれていることを意味します。