C# List<T>进行多字段排序

C# List<T>进行多字段排序,第1张

概述C# List<T>进行多字排序

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

//调用方法  IList<class> List = new List<class>();  //排序字段string[] property = new string[] { "column1","column2" };//对应排序字段的排序方式bool[] sort =new bool[]{ false,false };  //对 List 排序List = new IListSort<class>(List,property,sort).sort();      using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Reflection;  namespace Common{    /// <summary>    /// IList排序类    /// </summary>    /// <typeparam name="T"></typeparam>    public class IListSort<T>    {        private string[] _propertyname;        private bool[] _sortBy;        private IList<T> _List;          /// <summary>        /// 构造函数        /// </summary>        /// <param name="List">排序的IList</param>        /// <param name="propertyname">排序字段属性名</param>        /// <param name="sortBy">true升序 false 降序 不指定则为true</param>        public IListSort(IList<T> List,string[] propertyname,bool[] sortBy)        {            _List = List;            _propertyname = propertyname;            _sortBy = sortBy;        }        /// <summary>        /// 构造函数        /// </summary>        /// <param name="List">排序的IList</param>        /// <param name="propertyname">排序字段属性名</param>        /// <param name="sortBy">true升序 false 降序 不指定则为true</param>        public IListSort(IList<T> List,string[] propertyname)        {            _List = List;            _propertyname = propertyname;            for (int i = 0; i < _propertyname.Length; i++)            {                _sortBy[i] = true;            }        }          /// <summary>        /// IList        /// </summary>        public IList<T> List        {            get { return _List; }            set { _List = value; }        }          /// <summary>        /// 排序字段属性名        /// </summary>        public string[] Propertyname        {            get { return _propertyname; }            set { _propertyname = value; }        }          /// <summary>        /// true升序 false 降序        /// </summary>        public bool[] SortBy        {            get { return _sortBy; }            set { _sortBy = value; }        }          /// <summary>        /// 排序,插入排序方法        /// </summary>        /// <returns></returns>        public IList<T> Sort()        {            if (_List.Count == 0) return _List;            for (int i = 1; i < _List.Count; i++)            {                T t = _List[i];                int j = i;                while ((j > 0) && Compare(_List[j - 1],t) < 0)                {                    _List[j] = _List[j - 1];                    --j;                }                _List[j] = t;            }            return _List;        }          /// <summary>        /// 比较大小 返回值 小于零则X小于Y,等于零则X等于Y,大于零则X大于Y        /// </summary>        /// <param name="x"></param>        /// <param name="y"></param>        /// <returns></returns>        private int Compare(T x,T y)        {            int i =0;            //检查属性名            for (i = 0; i < _propertyname.Length; ++i)            {                if (string.IsNullOrEmpty(_propertyname[i])) throw new ArgumentNullException("没有指字对象的排序字段属性名!");            }              //取属性的属性            PropertyInfo[] property = new PropertyInfo[_propertyname.Length];            for (i = 0; i < _propertyname.Length; ++i)            {                property[i] = typeof(T).GetProperty(_propertyname[i]);                if (property[i] == null) throw new ArgumentNullException("在对象中没有找到指定属性!");            }              int compare = 0;            for (i = 0; i < _propertyname.Length;++i)            {                compare = CompareOne(x,y,property[i],_sortBy[i]);                if (compare != 0) return compare;            }            return compare;        }          private int CompareOne(T x,T y,PropertyInfo property,bool sortBy)        {            switch (property.PropertyType.ToString())            {                case "system.int32":                    int int1 = 0;                    int int2 = 0;                    if (property.GetValue(x,null) != null)                    {                        int1 = Convert.ToInt32(property.GetValue(x,null));                    }                    if (property.GetValue(y,null) != null)                    {                        int2 = Convert.ToInt32(property.GetValue(y,null));                    }                    if (sortBy)                    {                        return int2.Compareto(int1);                    }                    else                    {                        return int1.Compareto(int2);                    }                    break;                case "System.Double":                    double double1 = 0;                    double double2 = 0;                    if (property.GetValue(x,null) != null)                    {                        double1 = Convert.Todouble(property.GetValue(x,null) != null)                    {                        double2 = Convert.Todouble(property.GetValue(y,null));                    }                    if (sortBy)                    {                        return double2.Compareto(double1);                    }                    else                    {                        return double1.Compareto(double2);                    }                    break;                case "System.String":                    string string1 = string.Empty;                    string string2 = string.Empty;                    if (property.GetValue(x,null) != null)                    {                        string1 = property.GetValue(x,null).ToString();                    }                    if (property.GetValue(y,null) != null)                    {                        string2 = property.GetValue(y,null).ToString();                    }                    if (sortBy)                    {                        return string2.Compareto(string1);                    }                    else                    {                        return string1.Compareto(string2);                    }                    break;                case "System.DateTime":                    DateTime DateTime1 = DateTime.Now;                    DateTime DateTime2 = DateTime.Now;                    if (property.GetValue(x,null) != null)                    {                        DateTime1 = Convert.ToDateTime(property.GetValue(x,null) != null)                    {                        DateTime2 = Convert.ToDateTime(property.GetValue(y,null));                    }                    if (sortBy)                    {                        return DateTime2.Compareto(DateTime1);                    }                    else                    {                        return DateTime1.Compareto(DateTime2);                    }                    break;            }            return 0;        }    }}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C# List<T>进行多字段排序全部内容,希望文章能够帮你解决C# List<T>进行多字段排序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1238056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存