Array.Sort的执行步骤
主要问题是
1、既然Array.Sort本身具体有排序作用为什么还要参数 myComparer?难道仅仅是为了倒序?
2、myReverserClass::Compare(object x, object y)中的X,Y哪里来的?程序中并没有显式的调用compare函数
// 程序如下
using System;
using System.Collections;
public class SamplesArray
{
public class myReverserClass : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y )
{
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
public static void Main()
{
// Creates and initializes a new Array and a new custom comparer.
String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
IComparer myComparer = new myReverserClass();
// Displays the values of the Array.
Console.WriteLine( "The Array initially contains the following values:" );
PrintIndexAndValues( myArr );
// Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort( myArr, 1, 3, myComparer ); //【×××】
Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
PrintIndexAndValues( myArr );
// Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort( myArr, myComparer ); //【×××】
Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
PrintIndexAndValues( myArr );
}
public static void PrintIndexAndValues( String[] myArr )
{
for ( int i = 0; i < myArr.Length; i++ )
{
Console.WriteLine( " [{0}] : {1}", i, myArr[i] );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The Array initially contains the following values:
[0] : The
[1] : QUICK
[2] : BROWN
[3] : FOX
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog
After sorting a section of the Array using the reverse case-insensitive comparer:
[0] : The
[1] : QUICK
[2] : FOX
[3] : BROWN
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog
After sorting the entire Array using the reverse case-insensitive comparer:
[0] : the
[1] : The
[2] : QUICK
[3] : over
[4] : lazy
[5] : jumps
[6] : FOX
[7] : dog
[8] : BROWN
*/
1、既然Array.Sort本身具体有排序作用为什么还要参数 myComparer?难道仅仅是为了倒序?
2、myReverserClass::Compare(object x, object y)中的X,Y哪里来的?程序中并没有显式的调用compare函数
// 程序如下
using System;
using System.Collections;
public class SamplesArray
{
public class myReverserClass : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y )
{
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
}
}
public static void Main()
{
// Creates and initializes a new Array and a new custom comparer.
String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
IComparer myComparer = new myReverserClass();
// Displays the values of the Array.
Console.WriteLine( "The Array initially contains the following values:" );
PrintIndexAndValues( myArr );
// Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort( myArr, 1, 3, myComparer ); //【×××】
Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
PrintIndexAndValues( myArr );
// Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort( myArr, myComparer ); //【×××】
Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
PrintIndexAndValues( myArr );
}
public static void PrintIndexAndValues( String[] myArr )
{
for ( int i = 0; i < myArr.Length; i++ )
{
Console.WriteLine( " [{0}] : {1}", i, myArr[i] );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The Array initially contains the following values:
[0] : The
[1] : QUICK
[2] : BROWN
[3] : FOX
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog
After sorting a section of the Array using the reverse case-insensitive comparer:
[0] : The
[1] : QUICK
[2] : FOX
[3] : BROWN
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dog
After sorting the entire Array using the reverse case-insensitive comparer:
[0] : the
[1] : The
[2] : QUICK
[3] : over
[4] : lazy
[5] : jumps
[6] : FOX
[7] : dog
[8] : BROWN
*/
...全文
相关推荐
quick-sort-using-array-of-pointer.rar_Quick
quick sort using array of pointer
php关于array_multisort多维数组排序的使用说明
对于PHP语言中的多维数组排序时最为复杂的一个排序方式。我们在实际编码中将会用到PHP函数array_multisort()来实现这一复杂的排序。
PHPCMS V9用array_unique函数解决采集网址重复的办法
本文主要介绍了用array_unique函数解决采集网址重复的办法.请大家参考一下
java代码-array 的 sort fill equals 用法
java代码-array 的 sort fill equals 用法
php代码-array_multisort + trace
php代码-array_multisort + trace


