自定義集合類排序(Sort())方法隨筆
ASP.NET StartKit TimeTracker中定義了很多自定義集合類
例如:
UsersCollection
TimeEntriesCollection
等等
他們都是ArrayList類的子類
例如:
TimeEntriesCollection存放自定義類TimeEntry
這些自定義集合類都實現了排序方法Sort
先看我寫的一段代碼:
using System;
using System.Collections;
namespace ArrayListSort
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList a=new ArrayList(8);
a.Add(111);
a.Add(2);
a.Sort();
IEnumerator iEnumerator=a.GetEnumerator();
while( iEnumerator.MoveNext() )
{
Console.WriteLine(iEnumerator.Current.ToString() );
}
ArrayList b=new ArrayList(8);
b.Add(new class2("wo"));
b.Add( new class2("ai"));
b.Add( new class2("i"));
b.Sort();
IEnumerator iiEnumerator=b.GetEnumerator();
while( iiEnumerator.MoveNext() )
{
Console.WriteLine(((class2)iiEnumerator.Current).A );
}
Console.ReadLine();
}
}
public class class2
{
public class2( string str)
{
this.a=str;
}
string a;
public string A
{
get
{
return a;
}
}
}
}