美文网首页
基于反射、泛型的不定参数、不定类型的排序

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

作者: 祝你万事顺利 | 来源:发表于2019-06-13 14:01 被阅读0次

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

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

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

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;
        }
    }

相关文章

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

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

  • Swift Tour Learn (十二) -- Swift 语

    本章将会介绍 泛型所解决的问题泛型函数类型参数命名类型参数泛型类型扩展一个泛型类型类型约束关联类型泛型 Where...

  • go 泛型

    go 泛型 1. 类型参数(Type parameters) Go语言的泛型(Generic)叫做类型参数。泛型可...

  • 抽象工厂模式创建对象

    结合反射应用,使用class.forName反射创建对象,通过泛型约束参数类型新产品只需实现CommonAPI即可...

  • C#规范整理·泛型委托事件

    基于泛型,我们得以将类型参数化,以便更大范围地进行代码复用。同时,它减少了泛型类及泛型方法中的转型,确保了类型安全...

  • C#规范整理·泛型委托事件

    基于泛型,我们得以将类型参数化,以便更大范围地进行代码复用。同时,它减少了泛型类及泛型方法中的转型,确保了类型安全...

  • Java学习 Day7

    1.动态参数(不定长参数): 只能作为方法的参数。参数的个数不定。 语法:数据类型...变量名; (1)不定长参数...

  • 二维数组

    1.动态参数(不定长参数): 只能作为方法的参数。参数的个数不定。 语法:数据类型...变量名; (1)不定长参数...

  • 泛型学习

    1.泛型是Java中参数化类型的方式。将类型也作为一种参数进行传递。2.它有泛型的方法,泛型参数,泛型类。3.泛型...

  • 关于反射的使用

    反射中获取泛型参数信息

网友评论

      本文标题:基于反射、泛型的不定参数、不定类型的排序

      本文链接:https://www.haomeiwen.com/subject/omqjfctx.html