zoukankan      html  css  js  c++  java
  • 泛型IComparer<T>排序

        class Program
        {
            static void Main(string[] args)
            {
                GetListTest();
            }
    
            private static void GetListTest()
            {
                DBHelper dbHelper = DBHelper.GetInstance();
                DataSet ds = dbHelper.GetSqlDataSet("SELECT name,age FROM tbl_test",null);
                
                List<Person> listPerson = new List<Person>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Person model = new Person();
                    model.Name = ds.Tables[0].Rows[i]["name"].ToString();
                    model.Age = ds.Tables[0].Rows[i]["age"].ToString();
                    listPerson.Add(model);
                }
    
                //年龄排序
                Console.WriteLine("----年龄排序----");
                listPerson.Sort(new SortAge());
                for (int i = 0; i < listPerson.Count; i++)
                {
                    Console.WriteLine(listPerson[i].Name + ":" + listPerson[i].Age);
                }
                Console.WriteLine("");
                //姓名排序
                Console.WriteLine("----姓名排序----");
                listPerson.Sort(new SortName());
                for (int i = 0; i < listPerson.Count; i++)
                {
                    Console.WriteLine(listPerson[i].Name + ":" + listPerson[i].Age);
                }
            }
    
        }
    
        /// <summary>
        /// 排序实体
        /// </summary>
        class Person
        {
            private string name;
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
    
            private string age;
            /// <summary>
            /// 年龄
            /// </summary>
            public string Age
            {
                get { return age; }
                set { age = value; }
            }
        }
    
        /// <summary>
        /// 年龄排序
        /// </summary>
        class SortAge :IComparer<Person>
        {
            public int Compare(Person x, Person y)
            {
                return x.Age.CompareTo(y.Age);
            }
        }
    
        /// <summary>
        /// 姓名排序
        /// </summary>
        class SortName : IComparer<Person>
        {
            public int Compare(Person x, Person y)
            {
                return x.Name.CompareTo(y.Name);
            }
        }
  • 相关阅读:
    基础最短路(模板 bellman_ford)
    UVA-12304 Race(递推)
    How do you add?(递推)
    Coconuts, Revisited(递推+枚举+模拟)
    UVA-10726 Coco Monkey(递推)
    UVA-10995 Educational Journey
    UVA-10339 Watching Watches
    【React】377- 实现 React 中的状态自动保存
    【JS】376- Axios 使用指南
    【Nodejs】375- 如何加快 Node.js 应用的启动速度
  • 原文地址:https://www.cnblogs.com/myjacky/p/3227061.html
Copyright © 2011-2022 走看看