利用"委托"实现对象实例按"多字段嵌套"排序

类别:.NET开发 点击:0 评论:0 推荐:

/*

利用"委托"实现对象实例按"多字段嵌套"排序

 

(不知道大家看懂题目了? 功能还是挺实用的!)

本文实现 Person 类的实例们按 年龄、身高、体重等字段嵌套排序!

就像 SQL 查询的 Order By 字段1,字段2,...,字段n 子句的效果一样!

 

*/

 

using System;

class Class1 //客户程序

{

    static void Main()

    {

        Person[] Persons =

                            {

                                // 姓名、年龄、身高、体重

                                 new Person("张栢芝", 71,178,49)

                                ,new Person("章子怡", 23,177,33)

                                ,new Person("周  迅", 71,180,80)

                                ,new Person("徐静蕾", 12,150,70)

                                ,new Person("赵  薇", 23,166,60)

                                ,new Person("宋丹丹", 50,183,50)

                                ,new Person("翠花儿", 23,177,34)

                                ,new Person("赵丽蓉", 50,184,40)

                                ,new Person("郭晶晶", 50,184,41)

                            };

 

        //"BubbleSorter.CompareOp 委托" 类型数组的元素顺序,规定了嵌套排序的顺序,:

        //先按"年龄(Age)"排序,如果一样再按"身高(Height)",如果还一样再按"体重(Weight)"!

        //注意这只是排序的结果而以,如果要按此思路编下去,就有"递归"的意思了,然而层次却无法确定!

        Sorter.CompareOp[] PersonCompareOp =

                                            {

                                                // 你可以调换元素顺序,来改变排序的字段顺序

                                                 new Sorter.CompareOp(Person.PrevIsOlderThanNext)

                                                ,new Sorter.CompareOp(Person.PrevIsHigherThanNext)

                                                ,new Sorter.CompareOp(Person.PrevIsHeavierThanNext)

                                            };

 

        // 转到 Person BubbleSorter 类的实现!

        Sorter.BubbleSort(Persons, PersonCompareOp);

 

        for (int i = 0; i < Persons.Length; i++)

            Console.WriteLine(Persons[i].ToString());

 

        Console.ReadLine();

    }

}

 

class Person

{

    private string _name;

    private int _age;

    private int _weight;

    private int _height;

 

    public Person(string Name,int Age,int Height,int Weight)

    {

        this._name = Name;

        this._age = Age;

        this._height = Height;

        this._weight = Weight;

    }

 

    public override string ToString()

    {

        return string.Format(_name + ", {0:N}, {1:N}, {2:N}", _age,_height,_weight);

    }

 

    // 下面"额外"定义了若干与委托签名一致匹配的静态方法,供"委托调用"

    // 返回值为"真"时,代表 "前一元素 <= 后一元素" ,否则为"假"!

    // 只要参照这里,我们可以为类添加的排序功能并定义排序规则!

    // 多么简单啊! 剩下的工作就都留给 "排序功能(工具)类" 去做吧!

    public static bool PrevIsOlderThanNext(Object Prev, Object Next)

    {

        // 这里一定要用 "小于等于(<=)","等于"就是为了实现:

        // 如果两元素前一规则(如: 年龄)排序相等,就按下一规则(如: 身高)排序

        // 如果 "前一元素 <= 后一元素" ,返回"真",返回"假"!

        return (((Person) Prev)._age <= ((Person) Next)._age);

    }

    public static bool PrevIsHigherThanNext(Object Prev, Object Next)

    {

        return(((Person) Prev)._height <= ((Person) Next)._height);

    }

    public static bool PrevIsHeavierThanNext(Object Prev, Object Next)

    {

        return(((Person) Prev)._weight <= ((Person) Next)._weight);

    }

}

 

// "排序功能(工具)类"

class Sorter

{

    // 声明委托

    public delegate bool CompareOp(object X, object Y);

   

    //本排序的基本算法就是冒泡法最常见简单易懂,属于"交换"排序法的一种!

    public static void BubbleSort(object[] A, CompareOp[] MoreThanMethod) //MoreThanMethod 就是排序的字段顺序

    {

        for (int k = 0; k < MoreThanMethod.Length ; k ++) // 遍历委托数组

        {

            for (int i = 0 ; i < A.Length ; i ++) // 冒泡法外层循环

            {

                for (int j = i + 1 ; j < A.Length ; j ++) // 冒泡法内层循环

                {

                    bool b = true;

                    for (int m = 0; m <= k ; m ++)

                    {

                        // 如果两元素"前一规则(如: 年龄)"值相等,就按下一规则(如: 身高)比较排序

                        b = b && MoreThanMethod[m](A[j],A[i]);

                    }

                    if (b) //冒泡法交换元素的条件

                    {

                        // 冒泡法交换元素

                        object o = A[i];

                        A[i] = A[j];

                        A[j] = o;

                    }

                }

            }

        }

    }

}

 

// 好了就讲到这里了!,有兴趣的读者可以改用别的排序算法,期待在评论回复中"貂尾续狗"!

 

本文地址:http://com.8s8s.com/it/it43149.htm