代码阅读总结之ASP.NET StartKit TimeTracker(自定义集合类排序(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;
            }

        }

        
    }



}


运行结果是

a的排序结果出来了
b的排序出现了错误

为什么会出现这样的结果?
ArrayList的Sort方法是利用了接口IComparable来进行的排序
这就是说ArrayList中的每个对象需要实现IComparable接口
int,string等系统已定义类型已经实现了IComparable接口
a.Sort()方法运行是正确的
b.Sort();由于没有实现IComparable接口出现错误

现在修改class2代码,程序就可以正确运行

public class class2:IComparable
    
{

        
public class2( string str)
        
{
            
this.a=str;
        }

        
        
string a;

        
public string A
        
{
            
get
            
{
                
return a;
            }

        }



        
public int CompareTo(object o)
        
{
            
return this.A.CompareTo( ((class2)o).A ); 
        }

        
    }



其实还有一种方法实现排序
利用Sort(IComparer)
定义类实现接口System.Collections.IComparer
代码如下:

public class class2_sort:System.Collections.IComparer
    
{
        
//是否为升序
        bool isAsc;
        
        
//默认排序方式为升序
        public class2_sort()
        
{
            isAsc
=true;
        }


        
public class2_sort(bool b)
        
{
            isAsc
=b;
        }


        
public     int Compare(object x ,object y)
        
{
            
if (isAsc)
            
{
                
//升序
                return ((class2)x).A.CompareTo(((class2)y).A);
            }

            
else
            
{
                
//降序
                return ((class2)y).A.CompareTo(((class2)x).A);
            }

        }

    }


b.Sort( new class2_sort(false));
就可以实现降循序排序

个人觉得此方法灵活性高
推荐使用

小总结:
1.若要对自己的对象的数组进行排序,请在该对象上实现 IComparable 接口。
必须在该对象中定义一个 CompareTo 方法,以指定比较该对象的方式。
2.若要定义比较对象的特定方式,请创建一个实现了 IComparer 接口的类。
在该类中,必须包含一个 Compare 方法,该方法可接受要比较的两个对象。
它有助于您了解想要比较对象的方式以及允许哪些对象使用所定义的接口。

 

希望上面提到的知识对你有所提示
当然欢迎交流和指正

author:aierong
blog:http://www.cnblogs.com/aierong
email:aierong@126.com

posted @ 2005-03-14 14:23  aierong  阅读(2282)  评论(1编辑  收藏  举报