请问关于这个排序的函数,他的内部执行机制是怎样的?
譬如:foreach(int x in arr){...}
它的内部是实现:
IEnumerator enumerator=arr.Getnumerator();
while(enumerator.MoveNext())
{int x=(int)enumerator.Current;}而关于这个Array.sort()是怎样的?我只知道它要执行的是接口IComparer和IComparable,但是具体怎么执行接口里的方法就不清楚了,请教请教
譬如:foreach(int x in arr){...}
它的内部是实现:
IEnumerator enumerator=arr.Getnumerator();
while(enumerator.MoveNext())
{int x=(int)enumerator.Current;}而关于这个Array.sort()是怎样的?我只知道它要执行的是接口IComparer和IComparable,但是具体怎么执行接口里的方法就不清楚了,请教请教
解决方案 »
- C# C/S程序如何上传一个文件到网站服务器上
- 怎样在mobile项目中了解conpact数据库
- 请问在C#查询结果如何导出EXCEL
- DataTable
- 急切等待!!!:未处理的“System.NotSupportedException”类型的异常出现在 mscorlib.dll 中。
- 通宵做设计,有一简单问题,有关截取字符串的,高手指点,谢谢
- 使用了定时关机以后,能不能在定时关机后到以后一段时间不让他开机
- 刪除dataView中RowFilter出來的所有行
- 各位,如何部署引用了word的应用程序?
- .net B2版中的msdn有无中文内容啊?如果没有,现在市面上是否有关于.net的msdn啊?
- Array.Sort的执行步骤
- 急寻QQ升级记牌器源代码
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 default comparer.
Array.Sort( myArr, 1, 3 );
Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
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 default comparer.
Array.Sort( myArr );
Console.WriteLine( "After sorting the entire Array using the default 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] : dogAfter sorting a section of the Array using the default comparer:
[0] : The
[1] : BROWN
[2] : FOX
[3] : QUICK
[4] : jumps
[5] : over
[6] : the
[7] : lazy
[8] : dogAfter 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] : dogAfter sorting the entire Array using the default comparer:
[0] : BROWN
[1] : dog
[2] : FOX
[3] : jumps
[4] : lazy
[5] : over
[6] : QUICK
[7] : the
[8] : TheAfter 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*/
譬如:foreach(int x in arr){...}
它的内部是实现:
IEnumerator enumerator=arr.Getnumerator();
while(enumerator.MoveNext())
{int x=(int)enumerator.Current;}
。。
那么调用Array.Sort( myArr, 1, 3 );
它的内部是怎么实现的??我想从IComparer和ICompartor两个接口中分别知道是怎么来实现的??
public class myReverserClass : IComparer { // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare( Object x, Object y ) {
return( (new CaseInsensitiveComparer()).Compare( y, x ) );
} }说了这么多.不知道你明白没有.-_-!
那它的具体过程是怎样的?譬如:foreach(int x in arr){...} //arr是一个迭代器
它的内部是实现:
IEnumerator enumerator=arr.Getnumerator();
while(enumerator.MoveNext())
{int x=(int)enumerator.Current;} 那么调用Array.sort(aa);//aa是一个数组
以后,它的内部又是怎样的?不好意思,可能是我的表述有点问题