码迷,mamicode.com
首页 > 其他好文 > 详细

ICompare接口

时间:2016-12-05 14:32:21      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:sorted   ext   对比   esc   字母   实现   相同   默认   nbsp   

using System;
using System.Collections;
namespace ConsoleEnum
{
    public class car : IComparable
    {
        //  嵌套类的开始。

        // 按年升序
        private class sortYearAscendingHelper : IComparer
        {
            //实现接口定义的方法
            int IComparer.Compare(object a, object b)
            {
                car c1 = (car)a;
                car c2 = (car)b;

                if (c1.year > c2.year)
                    return 1;

                if (c1.year < c2.year)
                    return -1;

                else
                    return 0;
            }
        }

        // 按年降序
        private class sortYearDescendingHelper : IComparer
        {
            int IComparer.Compare(object a, object b)
            {
                car c1 = (car)a;
                car c2 = (car)b;

                if (c1.year < c2.year)
                    return 1;

                if (c1.year > c2.year)
                    return -1;

                else
                    return 0;
            }
        }

        // 按制造商字母降序
        private class sortMakeDescendingHelper : IComparer
        {
            int IComparer.Compare(object a, object b)
            {
                car c1 = (car)a;
                car c2 = (car)b;
                
                return String.Compare(c2.make, c1.make);
            }
        }

        // 嵌套类结束

        private int year;
        private string make;

        //构造函数
        public car(string Make, int Year)
        {
            make = Make;
            year = Year;
        }

        //属性
        public int Year
        {
            get { return year; }
            set { year = value; }
        }

        public string Make
        {
            get { return make; }
            set { make = value; }
        }

        // 实现IComparable接口来提供默认的排序顺序的对比。
       // 将当前实例与同一类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例在排序顺序中的位置是位于另一个对象之前、之后还是与其位置相同。
        int IComparable.CompareTo(object obj)
        {
            car c = (car)obj;
            return String.Compare(this.make, c.make);
        }

        // 返回排序辅助IComparer对象的方法。
        public static IComparer sortYearAscending()
        {
            return (IComparer)new sortYearAscendingHelper();
        }

        // 返回排序辅助IComparer对象的方法。
        public static IComparer sortYearDescending()
        {
            return (IComparer)new sortYearDescendingHelper();
        }

        // 返回排序辅助IComparer对象的方法。
        public static IComparer sortMakeDescending()
        {
            return (IComparer)new sortMakeDescendingHelper();
        }

    }
}

  

 

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

namespace ConsoleEnum
{
    class host
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Create an arary of car objects.      
            car[] arrayOfCars = new car[6]
            {
            new car("Ford",1992),
            new car("Fiat",1988),
            new car("Buick",1932),
            new car("Ford",1932),
            new car("Dodge",1999),
            new car("Honda",1977)
            };


            Console.WriteLine("Array - Unsorted\n");

            foreach (car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);

            // 演示时用“默认”排序的数组排序。
            Array.Sort(arrayOfCars);
            Console.WriteLine("\nArray - Sorted by Make (Ascending - IComparable)\n");

            foreach (car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);

            // Demo ascending sort of numeric value with IComparer.
                       //要排序的数组,自定义的排序规则
            Array.Sort(arrayOfCars, car.sortYearAscending());
            Console.WriteLine("\nArray - Sorted by Year (Ascending - IComparer)\n");

            foreach (car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);

            // Demo descending sort of string value with IComparer.
            Array.Sort(arrayOfCars, car.sortMakeDescending());
            Console.WriteLine("\nArray - Sorted by Make (Descending - IComparer)\n");

            foreach (car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);

            // Demo descending sort of numeric value using IComparer.
            Array.Sort(arrayOfCars, car.sortYearDescending());
            Console.WriteLine("\nArray - Sorted by Year (Descending - IComparer)\n");

            foreach (car c in arrayOfCars)
                Console.WriteLine(c.Make + "\t\t" + c.Year);

            Console.ReadLine();
        }
    }
}

  

 

ICompare接口

标签:sorted   ext   对比   esc   字母   实现   相同   默认   nbsp   

原文地址:http://www.cnblogs.com/hao-1234-1234/p/6133586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!