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(); }}
正反排序指的就是升降序了, IComparer.Compare( Object x, Object y )可以是自己定义的类的某一个属性吗?
class Test:IComparable { private int a; #region IComparable 成员 public int CompareTo(object obj) { Test t = (Test)obj; return this.a - t.a; } #endregion } 给你一个最简单的.这样就可以使用sort了
//IComparer.Compare( Object x, Object y )可以是自己定义的类的某一个属性吗? 是方法不是属性
如果要对对象排序用sort方法。但对象必须实现IComparable接口。否则排序无效。
如果要顺序反转则用Reverse方法。
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(); }}
自定义.Compare比较方法
创建比较器对象,调用重载的ArrayList.Sort(IComparer)方法,传入比较器对象
IComparer.Compare( Object x, Object y )可以是自己定义的类的某一个属性吗?
{
private int a; #region IComparable 成员 public int CompareTo(object obj)
{
Test t = (Test)obj;
return this.a - t.a;
} #endregion
}
给你一个最简单的.这样就可以使用sort了
是方法不是属性