Sort()小问题

Comparison 是一个delegate
几个常用的例子:
<1>

class Program
{
static void Main(string[] args)
{
int[] ia = new int[] { 1, 5, 2, 4, 3, 7, 6 };
Array.Sort(ia);
foreach (int i in ia)
Console.WriteLine(i);
}
}

<2>

class Program
{
static void Main(string[] args)
{
List il = new List(){1,3,4,2,6,7,5};
il.Sort();

foreach (int i in il)
Console.WriteLine(i);
}

<3>升序

class Program
{
private static int Cmp(int x, int y)
{
if(x == y)
return 0;
else if(x < y) return -1;
else return 1;
}

static void Main(string[] args)
{
List il = new List(){1,3,4,2,6,7,5};
il.Sort(Cmp);

foreach (int i in il)
Console.WriteLine(i);
}


class Program
{
private static int Cmp(int x, int y)
{
if(x == y)
return 0;
else if(x < y) return -1;
else return 1;
}

static void Main(string[] args)
{
int[] intArray = new int[]{1,3,4,2,6,7,5};
Array.Sort(intArray, Cmp);
foreach (int i in intArray)
Console.WriteLine(i);
}
}

你可能感兴趣的:(C,C++,C#)