zoukankan      html  css  js  c++  java
  • IComparable IComparer Array排序

    蛋疼  还是蛋疼  无聊写点基础东西

    控制台程序

    实现对一个类的排序

    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Text;

    namespace ArrayEX
    {
        public class Person : IComparable<Person>
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public Person(string LastName,string FirstName)
            {
                this.LastName = LastName;
                this.FirstName = FirstName;
            }

            public int CompareTo(Person obj)
            {   
                int result = this.LastName.CompareTo(obj.LastName);
                if (result == 0)
                {
                    result = this.FirstName.CompareTo(obj.FirstName);
                }
                return result;
            }
        }

        public class PersonComparer : IComparer<PersonComparer>
        {
            public string LastName { get; set; }
            public string FirstName { get; set; }
            public PersonComparer(string LastName, string FirstName)
            {
                this.LastName = LastName;
                this.FirstName = FirstName;
            }
            public int Compare(PersonComparer x,PersonComparer y)
            {
                return x.LastName.CompareTo(y.LastName);
            }
        }
    }

    //////////////////////////////////////

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ArrayEX
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person[] persons ={
                                     new Person("Emerson","Fittipaldi"),
                                     new Person("Niki","Lauda"),
                                     new Person("Ayrton","Senna"),
                                     new Person("Michael","Schumacher"),
                                 };
                Array.Sort(persons);
                foreach (Person p in persons)
                {
                    Console.WriteLine("{0}\t{1}", p.LastName, p.FirstName);
                }

                Console.WriteLine("/////////////////////////");

                PersonComparer[] personcomparer ={
                                                    new PersonComparer("Emerson","Fittipaldi"),
                                                    new PersonComparer("Niki","Lauda"),
                                                    new PersonComparer("Michael","Schumacher"),
                                                    new PersonComparer("Ayrton","Senna"),
                                                };
                Array.Sort(personcomparer, personcomparer);
                foreach (PersonComparer p in personcomparer)
                {
                    Console.WriteLine("{0}\t{1}", p.LastName, p.FirstName);
                }
                Console.Read();
            }
        }
    }

    所有数值类型(如 Int32Double)均实现 IComparable,这一点与 StringCharDateTime 是相同的。 此外,自定义类型还应提供自己的 IComparable 实现,以便允许对象实例进行排序。

    IComparable 接口

    此接口由具有可排序值的类型实现。 它要求实现类型定义单个方法 CompareTo(Object),该方法指示当前实例在排序顺序中的位置是位于同一类型的另一个对象之前、之后还是与其位置相同。 实例的 IComparable 实现由 Array.SortArrayList.Sort 等方法自动调用。

  • 相关阅读:
    TYPE_SCROLL_INSENSITIVE is not compatible with CONCUR_UPDATABLE
    with admin option /with grant option
    通过动态SQL语句创建游标
    Vue:在vue-cli中使用Bootstrap
    Vue:$set和$delete
    Vue:如何在vue-cli中创建并引入自定义组件
    MVC教程四:Controller向View传值的几种方式
    Vue:生命周期
    Vue.js常用指令:v-model
    vue:过滤器
  • 原文地址:https://www.cnblogs.com/Kiros/p/1923264.html
Copyright © 2011-2022 走看看