帮助文档上的一段代码: using System; using System.Collections;public class SamplesArrayList { 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 ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "QUICK" ); myAL.Add( "BROWN" ); myAL.Add( "FOX" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" );
// Displays the values of the ArrayList. Console.WriteLine( "The ArrayList initially contains the following values:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the default comparer. myAL.Sort( 1, 3, null ); Console.WriteLine( "After sorting from index 1 to index 3 with the default comparer:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the reverse case-insensitive comparer. IComparer myComparer = new myReverserClass(); myAL.Sort( 1, 3, myComparer ); Console.WriteLine( "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:" ); PrintIndexAndValues( myAL ); }
public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; System.Collections.IEnumerator myEnumerator = myList.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current ); Console.WriteLine(); }}
ArrayList 中方法Sort 可实现排序 使用指定的比较器对部分 ArrayList 中的元素进行排序。 [C#] public virtual void Sort( int index, int count, IComparer comparer ); 参数 index 要排序的范围的从零开始的起始索引。 count 要排序的范围的长度。 comparer 比较元素时要使用的 IComparer 实现。 - 或 - 若为空引用(Visual Basic 中为 Nothing),则使用每个元素的 IComparable 实现。
using System;
using System.Collections;public class SamplesArrayList { 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 ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "QUICK" );
myAL.Add( "BROWN" );
myAL.Add( "FOX" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );
// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the default comparer.
myAL.Sort( 1, 3, null );
Console.WriteLine( "After sorting from index 1 to index 3 with the default comparer:" );
PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the reverse case-insensitive comparer.
IComparer myComparer = new myReverserClass();
myAL.Sort( 1, 3, myComparer );
Console.WriteLine( "After sorting from index 1 to index 3 with the reverse case-insensitive comparer:" );
PrintIndexAndValues( myAL ); }
public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0;
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
Console.WriteLine(); }}
使用指定的比较器对部分 ArrayList 中的元素进行排序。
[C#]
public virtual void Sort(
int index,
int count,
IComparer comparer
);
参数
index
要排序的范围的从零开始的起始索引。
count
要排序的范围的长度。
comparer
比较元素时要使用的 IComparer 实现。
- 或 - 若为空引用(Visual Basic 中为 Nothing),则使用每个元素的 IComparable 实现。