前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于反射、泛型的不定参数、不定类型的排序

基于反射、泛型的不定参数、不定类型的排序

作者头像
祝你万事顺利
发布2019-06-13 19:06:58
1.8K0
发布2019-06-13 19:06:58
举报
文章被收录于专栏:Unity游戏开发Unity游戏开发

方法相关 参数: string数组 - 全部要比较的字段名称 bool数组 - 每一个字段升序排序还是降序排序 IList<T>集合 - 要排序的List

内部实现; 通过反射找到全部string数组中的字段,从第一个字段开始比较,如果相等,比较第二个字段,直到最后一个,通过bool控制升序还是降序。

使用方法 如Main函数中的使用,传入参数,调用IListSort类中的Sort方法,得到的传入的list就会进行排序。

代码语言:javascript
复制
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> personList = new List<Person>
            {
                new Person("kk",101,150,true),
                new Person("kklady",100,10,true),
                new Person("kkwomen",100,2,true),
                new Person("kkwomen",100,777,true),
                new Person("kkwomen",712,150,true),
                new Person("kkwomen",99,666,true),
            };
            string[] field = { "age", "height" };
            bool[] boolArray = { true, false };
            IListSort<Person> listSort = new IListSort<Person>(field, boolArray, personList);
            listSort.Sort();
            Console.WriteLine(personList);
        }
    }

    public class IListSort<T>
    {
        private string[] propertName;
        private bool[] sortBy;
        private IList<T> iList;

        public IListSort(string[] propertName, bool[] sortBy, IList<T> iList)
        {
            this.propertName = propertName;
            this.sortBy = sortBy;
            this.iList = iList;
        }

        public void Sort()
        {
            if (iList.Count <= 1)
            {
                return;
            }
            for (int i = 1; i < iList.Count; i++)
            {
                T t = iList[i];
                int j = i;//避免闭包
                while ((j > 0) && Compare(iList[j - 1], t) < 0)
                {
                    iList[j] = iList[j - 1];
                    --j;
                }
                iList[j] = t;
            }
            return;
        }

        /// <summary>
        /// 比较大小,前者小于后者返回1,相等返回0,前者大于后者返回-1
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <returns></returns>
        public int Compare(T t1, T t2)
        {
            for (int i = 0; i < propertName.Length; i++)
            {
                if (string.IsNullOrEmpty(propertName[i]))
                {
                    throw new ArgumentNullException("空排序属性异常");
                }
            }
            FieldInfo[] fieldInfos = new FieldInfo[propertName.Length];
            var type = typeof(T);
            for (int i = 0; i < propertName.Length; i++)
            {
                FieldInfo fieldInfo = type.GetField(propertName[i]);
                if (fieldInfo == null)
                {
                    throw new ArgumentNullException(propertName[i] + "字段不存在");
                }
                fieldInfos[i] = fieldInfo;
            }
            int compare = 0;
            for (int i = 0; i < propertName.Length; i++)
            {
                //compare等于一个排序方法
                compare = CompareInField(t1, t2, fieldInfos[i], sortBy[i]);
                if(compare != 0)//控制
                {
                    return compare;
                }
            }
            return compare;
        }

        private int CompareInField(T t1, T t2, FieldInfo fieldInfo, bool sortBy)
        {
            switch (fieldInfo.FieldType.ToString())
            {
                case "System.Int32"://这里是等于FieldType的全名
                    int int1 = 0;
                    int int2 = 0;
                    if (fieldInfo.GetValue(t1) != null)
                    {
                        int1 = Convert.ToInt32(fieldInfo.GetValue(t1));
                    }
                    if (fieldInfo.GetValue(t2) != null)
                    {
                        int2 = Convert.ToInt32(fieldInfo.GetValue(t2));
                    }
                    if (sortBy)
                    {
                        return int2.CompareTo(int1);//升序
                    }
                    else
                    {
                        return int1.CompareTo(int2);//降序
                    }
                case "System.Double":
                    double double1 = 0;
                    double double2 = 0;
                    if (fieldInfo.GetValue(t1) != null)
                    {
                        double1 = Convert.ToDouble(fieldInfo.GetValue(t1));
                    }
                    if (fieldInfo.GetValue(t2) != null)
                    {
                        double2 = Convert.ToDouble(fieldInfo.GetValue(t2));
                    }
                    if (sortBy)
                    {
                        return double2.CompareTo(double1);
                    }
                    else
                    {
                        return double1.CompareTo(double2);
                    }
                case "string":
                default:
                    break;
            }
            return 0;
        }
    }

    public class Person
    {
        public string name;
        public int age;
        public double height;
        public bool sex;

        public Person(string name, int age, double height, bool sex)
        {
            this.name = name;
            this.age = age;
            this.height = height;
            this.sex = sex;
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.06.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档