• 使用List<>泛型,怎么排序?


    使用List<>泛型,怎么排序?泛型好难理解啊
    #region   Using   directives  
       
      using   System;  
      using   System.Collections.Generic;  
      using   System.Text;  
       
      #endregion  
       
      namespace   Ch12Ex02  
      {  
            class   Program  
            {  
                  static   void   Main(string[]   args)  
                  {  
                           
                        //Collection<Animal>   animalCollection   =   new   Collection<Animal>();  
                        List<Animal>   animalCollection   =   new   List<Animal>();  
                        animalCollection.Add(new   Cow("Jack"));  
                        animalCollection.Add(new   Chicken("Vera"));  
                        //animalCollection.Sort();//此处编译错误,请问是怎么回事?  
                        foreach   (Animal   myAnimal   in   animalCollection)  
                        {  
                              myAnimal.Feed();  
                        }  
                        Console.ReadKey();  
                  }  
            }  
      }  

    ---------------------------------------------------
    对于范型来说必须指定比较器  
      你可以这样做:  
      1.   让Animal实现IComparable接口,实现CompareTo方法(假设以name来排序):  
      class   Animal   :   IComparable<Animal>  
      {  
          ...  
          public   int   CompareTo(Animal   other)  
          {  
                return   name.CompareTo(other.name);  
          }  
      }  
      这样直接调用sort就可以排序   list.Sort();  
      2.自己写ICompare比较器:  
      class   AnimalCompare   :   IComparer<Animal>  
      {  
              public   int   Compare(Animal   a,   Animal   b)  
              {  
                      return   a.name.CompareTo(b.name);  
              }  
      }  
      这样使用   list.Sort(new   AnimalCompare());

    -----------------------------------------------
    泛型集合的排序

    在工作中经常会遇到对象数组根据某个属性进行排序的问题。这里介绍一个方法。
      以汽车为例: public class Car: {
       private int weight;
      
       public int Weight
       {
       get { return weight; }
       set { weight = value; }
       }
       private string type;
      
       public string Type
       {
       get { return type; }
       set { type = value; }
       }
       }
      Car[] cars;现在需要排序,首先我们想根据Weight进行排序,大家自然会想到冒泡算法。不过这个肯定不是最好的,这里提供一个简便的方法。
      
      我们将类Car实现接口IComparable使其能够使用Array.Sort()。
      代码如下:
      
      
       public class Car:IComparable<Car>
       {
       private int weight;
      
       public int Weight
       {
       get { return weight; }
       set { weight = value; }
       }
       private string type;
      
       public string Type
       {
       get { return type; }
       set { type = value; }
       }
      
       IComparable 成员#region IComparable<Employee> 成员
      
       public int CompareTo(Car other)
       {
       if (this.weight == other.weight)
       return 0;
       if (this.weight > other.weight)
       return 1;
       return -1;
       }
      
       #endregion
       }实现该方法以后我们就可以直接使用如下代码来对cars进行排序了。
       Car[] arr = new Car[] {car1,car2,car3 };
       Array.Sort<Car>(arr);但是随着项目的发展的发展我们会迎来新的问题,我们现在又需要根据Type排序了,怎么办呢?
      不用担心我们只要使用一个最简单的Adapter模式就能解决这个问题
      下面我们来创建这个适配器:
       public class ComparaCarAdapter : IComparer<Car>
       {
      
       IComparer 成员#region IComparer<Car> 成员
      
       public int Compare(Car x, Car y)
       {
       return x.Type.CompareTo(y.Type);
       }
      
       #endregion
       }然后如此调用:
      Array.Sort<Car>(arr,new ComparaCarAdapter());但是这样如果属性很多,会产生很多的类,怎么办呢。那么利用反射吧。将ComparaCarAdapter改造为:
       public class ComparaCarAdapter : IComparer<Car>
       {
       string _progName = "";
       public ComparaCarAdapter(string progName)
       {
       _progName = progName;
       }
       IComparer 成员#region IComparer<Employee> 成员
      
       public int Compare(Car x, Car y)
       {
       Type t = typeof(Car);
       PropertyInfo pi = t.GetProperty(_progName);
       object xvalue = pi.GetValue(x, null);
       object yvalue = pi.GetValue(y, null);
       if (xvalue is string)
       {
       return ((string)xvalue).CompareTo((string)yvalue);
       }
       else
       {
       if (xvalue is int)
       return ((int)xvalue).CompareTo((int)yvalue);
       }
       throw new NotSupportedException();
       }
      
       #endregion
       }
      调用 Array.Sort<Car>(arr, new ComparaCarAdapter("Weight"));OK搞定,应该足够灵活了吧。

  • 相关阅读:
    web app变革之rem
    理解angularjs的作用域
    移动开发框架
    angularjs ng-repeat下验证问题
    10、长链接转短链接
    8、自定义菜单及菜单响应事件的处理
    9、多公众号集中管理
    7、消息管理-接收事件推送
    6、消息管理-普通消息接受处理
    5、用户和用户组管理-支持同步
  • 原文地址:https://www.cnblogs.com/bluecobra/p/1738595.html
Copyright © 2020-2023  润新知