日期:2014-05-17 浏览次数:21474 次
using System;
using System.Collections.Generic;
using System.Text;
namespace Comparer.SortObject
{
    /// <summary>
    /// List<>.Sort()的测试对象,类自己实现IComparable接口,
    /// 当需要对List中的SortTestObj1对象进行排序时,直接调用
    /// Sort()方法就可以了。
    /// </summary>
    public class SortTestObj1:IComparable
    {
        #region 类字段定义
        private int code;
        private string name;
        #endregion
        public SortTestObj1()
        {
            //
        }
        #region 属性定义
        public int Code
        {
            set { this.code = value; }
            get { return this.code; }
        }
        public string Name
        {
            set { this.name = value; }
            get { return this.name; }
        }
        #endregion
        #region 实现比较接口的CompareTo方法
        public int CompareTo(object obj)
        {
            int res = 0;
            try
            {
                SortTestObj1 sObj = (SortTestObj1)obj;
                if (this.code > sObj.code)
                {
                    res = 1;
                }
                else if (this.code < sObj.code)
                {
                    res = -1;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("比较异常", ex.InnerException);
            }
            return res;
        }
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Comparer.SortObject
{
    /// <summary>
    /// List<>.Sort(IComparer<(Of <(T>)>))的测试对象,类自己没有实现IComparable接口,
    /// 当需要对List中的SortTestObj2对象进行排序时,需要实例化一个SortTestObj2Camparer
    /// 类型的比较器,在比较器中指明排序类型(按Code排序还是按Name排序),然后调用
    /// xxxLst.Sort(SortTestObj2Camparer)方法就可以了。
    /// </summary>
    public class SortTestObj2
    {
        #region 类字段定义
        private int code;
        private string name;
        #endregion
        public SortTestObj2()
        {
            //
        }
        #region 属性定义
        public int Code
        {
            set { this.code = value; }
            get { return this.code; }
        }
        public string Name
        {
            set { this.name = value; }
            get { return this.name; }
        }
        #endregion
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Comparer.SortObject;
namespace Comparer.Camparer
{
    [Flags]
    //排序类型定义
    public enum Obj2SortKind { Code, Name }
    /// <summary>
    /// SortTestObj2类排序用的比较器,继承IComparer<>接口,
    /// 实现接口中的Compare()方法。
    /// </summary>
    public class SortTestObj2Camparer : IComparer<SortTestObj2>
    {
        #region 类字段定义
        private Obj2SortKind sortKind;
        #endregion
        #region 构造器
        public SortTestObj2Camparer(Obj2SortKind sk)
        {
            this.sortKind = sk;
        }
        #endregion
        #region IComparer接口比较方法的实现
        public int Compare(SortTestObj2 obj1, SortTestObj2 obj2)
        {
            int res = 0;
            if ((obj1 == null) && (obj2 == null))
            {
                return 0;
            }
            else if((obj1 != nul