C#中List的排序用法(Sort)--可指定排序字段

时间:2025-01-24 20:25:16

由于项目的原因用到了List<T> 泛型,要对自定义类数组或List进行排序,譬如:

List<User> userList;

ArrayList arrayList;

最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。

/// <summary>
    /// 对象比较时使用的信息类
    /// </summary>
    public struct ReverserInfo
    {
        /// <summary>
        /// 比较的方向,如下:
        /// ASC:升序
        /// DESC:降序
        /// </summary>
        public enum Direction
        {
            ASC = 0,
            DESC =1,
        };
        /// <summary>
        /// 字段名称
        /// </summary>
        public string name;
        /// <summary>
        /// 排序方式
        /// </summary>
        public Direction direction;
        
    }
    /// <summary>
    /// 排序
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Reverser<T> : IComparer<T>
    {
        private Type type = null;
        private ReverserInfo info;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">进行比较的类类型</param>
        /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="direction">比较方向(升序/降序)</param>
        public Reverser(Type type, string name, direction)
        {
            = type;
            = name;
            if (direction != )
                = direction;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="className">进行比较的类名称</param>
        /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="direction">比较方向(升序/降序)</param>
        public Reverser(string className, string name, direction) 
        {
            try
            {
                = (className, true);
                = name;
                = direction;
            }
            catch (Exception e){
                throw new Exception();
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="t">进行比较的类型的实例</param>
        /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="direction">比较方向(升序/降序)</param>
        public Reverser(T t, string name, direction)
        {
            = ();
            = name;
            = direction;
        }

        /// <summary>
        /// 实现IComparer的比较方法。
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <returns></returns>
        int IComparer<T>.Compare(T t1, T t2)
        {
            object x = (, | | , null, t1, null);
            object y = (, | | , null, t2, null);
            if ( != )
                Swap(ref x, ref y);
            return (new CaseInsensitiveComparer()).Compare(x, y);
        }
        /// <summary>
        /// 交换操作数
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private void Swap(ref object x, ref object y)
        {
            object temp = null;
            temp = x;
            x = y;
            y = temp;
        }

    }

使用样例

 public string Test()
 {
            List<TagNameModel> TagNameLists = new List<TagNameModel>();
            for (int i = 0; i < 5; i++)
            {
                TagNameModel tage = new TagNameModel() { BackgroundColor = "12", CategoryId = i, FontColor = "er", Name = (), TagId = 5 - i};
                (tage);
            }
            //调用排序类,按照CategoryId字段,升序排列
            Reverser<TagNameModel> reverser = new Reverser<TagNameModel>(new TagNameModel().GetType(), "CategoryId", );
            (reverser);
            return ();

        }