Joh*_*hnD 11
我写了一个测试程序来试试.它看起来确实比较()相同的元素与自身(至少相同的项目两次调用Compare()两次).在这个程序中,使用参数(2,2)调用Compare().
using System;
using System.Collections.Generic;
static class Program
{
class MyComparer : Comparer<int>
{
public override int Compare(int x, int y)
{
Console.WriteLine("Compare(" + x + ", " + y + ")");
if (x < y) return -1;
if (x > y) return 1;
return 0;
}
}
static void Main()
{
MyComparer comparer = new MyComparer();
List<int> list = new List<int> { 1, 2, 3 };
list.Sort(comparer);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Compare(1, 2)
Compare(1, 3)
Compare(2, 3)
Compare(1, 2)
Compare(2, 2)
Compare(2, 3)
Compare(2, 2)
Run Code Online (Sandbox Code Playgroud)