如何在c#中利用委托进行冒泡排序,下面我的这个代码有什么错误请各位高手指正:
Employee类    class Employee
    {
        private string name;
        private decimal salary;        public Employee(string name,decimal salary)
        {
            this.name=name;
            this.salary=salary;
        }        public override string ToString()
        {
            return string.Format("{0},{1:c}",name,salary);
        }        public static bool Comparison(object x, object y)
        {
            Employee e1 = (Employee)x;
            Employee e2 = (Employee)y;
            //return (e1.salary < e2.salary);
            return e1.salary < e2.salary;
        }
    }
BubbleSorter类: class BubbleSorter
    {
        static public void Sort(object[] sortArray, Comparison comparison) 
        {
            for (int i = 0; i < sortArray.Length; i++) 
            {
                for (int j = i + 1; j < sortArray.Length; j++)
                {
                    if (comparison(sortArray[j],sortArray[i])) 
                    {
                        object temp = sortArray[j];
                        sortArray[j] = sortArray[i];
                        sortArray[i] = temp;
                    }
                }
            }
        }
    }调用
 Employee[] employees =
            { 
                new Employee("Bugs Bunny",2000),
                new Employee("Elmer Fudd",10000),
                new Employee("Daffy Duck",250000),
                new Employee("Wiley Coyote",(decimal)1000000.38),
                new Employee("Foghorn Leghorn",23000),
                new Employee("RoadRunner",5000)
            };            //BubbleSorter.Sort(employees,Employee.CompareSalary);            BubbleSorter.Sort(employees,Employee.Comparison);
            
            foreach (var employee in employees)
            {
                Console.WriteLine(employee);
            }为什么会报:
错误 1 与“DelegatDemo.BubbleSorter.Sort(object[], Comparison)”最匹配的重载方法具有一些无效参数 E:\C#\vs08\Number\DelegatDemo\DelegatDemo\Program.cs 99 13 DelegatDemo错误 2 参数“2”: 无法从“方法组”转换为“Comparison” E:\C#\vs08\Number\DelegatDemo\DelegatDemo\Program.cs 99 41 DelegatDemo请各位高手指正,多谢!!!!

解决方案 »

  1.   


        class Employee
      {
          private string name;
          private decimal salary;      public Employee(string name,decimal salary)
          {
              this.name=name;
              this.salary=salary;
          }      public override string ToString()
          {
            return string.Format("{0},{1:c}",name,salary);
          }      public static bool Comparison(object x, object y)
          {
              Employee e1 = (Employee)x;
              Employee e2 = (Employee)y;
              //return (e1.salary < e2.salary);
              return e1.salary < e2.salary;
          }
      } class BubbleSorter
      {
         static public void Sort(object[] sortArray)  
          {
              for (int i = 0; i < sortArray.Length; i++)  
              {
                  for (int j = i + 1; j < sortArray.Length; j++)
                  {
                      if (Employee.Comparison(sortArray[j], sortArray[i]))  
                      {
                          object temp = sortArray[j];
                          sortArray[j] = sortArray[i];
                          sortArray[i] = temp;
                      }
                  }
              }
          }
      }//invoke            Employee[] employees =
              {  
              new Employee("Bugs Bunny",2000),
              new Employee("Elmer Fudd",10000),
              new Employee("Daffy Duck",250000),
              new Employee("Wiley Coyote",(decimal)1000000.38),
              new Employee("Foghorn Leghorn",23000),
              new Employee("RoadRunner",5000)
              };            //BubbleSorter.Sort(employees,Employee.CompareSalary);            BubbleSorter.Sort(employees);            foreach (var employee in employees)
                {
                    Console.WriteLine(employee);
                }
      

  2.   

    如果非要static public void Sort(object[] sortArray, Comparison comparison)  这么写 
    你要把Comparison从Employee中分离出来 定义一个类 然后定义static比较方法例:
    快排public interface IComp
    {
    int cmp(object o1, object o2);
    }public class Int32Comp : IComp
    {public Int32 cmp(object o1, object o2)
    {
      Int32 i1 = Convert.ToInt32(o1);
      Int32 i2 = Convert.ToInt32(o2);  if (i1 < i2)
      {
        return -1;
      }
      else if (i1 == i2)
      {
        return 0;
      }
      else
      {
        return 1;
      }
    }
    }public class Quicksort
    {
      private static Random rd = new Random();  public static void Sort<T>(T[] v, int left, int right, IComp cmp)
      {
        int i, last;    if (left >= right)
          return;    Swap<T>(v, left, rd.Next(left, right + 1));
        last = left;    for (i = left + 1; i < right + 1; i++)
        {
          if (cmp.cmp(v[i], v[left]) < 0)
          {
             Swap(v, ++last, i);
          }
         }     Swap<T>(v, left, last);
         Sort<T>(v, left, last - 1, cmp);
         Sort<T>(v, last + 1, right, cmp);
       }   public static void Swap<T>(T[] v, int i, int j)
       {
         T temp;     temp = v[i];
         v[i] = v[j];
         v[j] = temp;
       }
    }
    class Program
    {
      static void Main(string[] args)
      {    Int32[] arr = {101, 11, 2, 7, 4, 5, 3, 30};
        Quicksort.Sort(arr, 0, arr.Length - 1, new Int32Comp());    foreach (Int32 i in arr)
        {
           Console.WriteLine(i);
        }
      }
    }
    from http://www.cnblogs.com/Peter-Zhang/articles/1982846.html
      

  3.   

    你没有声明委托
      public delegate bool  Comparison(object x,object y);
      

  4.   


        public delegate bool Comparison<Type>(Type obj1, Type obj2);
        class Program
        {
            static void Main(string[] args)
            {
                Employee[] employees =
                {
                    new Employee("Bugs Bunny",2000),
                    new Employee("Elmer Fudd",10000),
                    new Employee("Daffy Duck",250000),
                    new Employee("Wiley Coyote",(decimal)1000000.38),
                    new Employee("Foghorn Leghorn",23000),
                    new Employee("RoadRunner",5000)
                };
                BubbleSorter.Sort<Employee>(employees, Employee.Comparison);            foreach (var employee in employees)
                {
                    Console.WriteLine(employee);
                }
            }
        }    class Employee
        {
            private string name;
            private decimal salary;        public Employee(string name, decimal salary)
            {
                this.name = name;
                this.salary = salary;
            }        public override string ToString()
            {
                return string.Format("{0},{1:c}", name, salary);
            }        public static bool Comparison(Employee x, Employee y)
            {
                return x.salary < y.salary;
            }
        }
        class BubbleSorter
        {
            static public void Sort<Type>(Type[] sortArray, Comparison<Type> comparison)
            {            for (int i = 0; i < sortArray.Length; i++)
                {
                    for (int j = i + 1; j < sortArray.Length; j++)
                    {
                        if (comparison(sortArray[j], sortArray[i]))
                        {
                            Type temp = sortArray[j];
                            sortArray[j] = sortArray[i];
                            sortArray[i] = temp;
                        }
                    }            }
            }
        }
      

  5.   

    Comparison在编译时被识别成《public static bool Comparison(object x, object y)》这类的方法
      

  6.   


    //测试<按名字排>
            BubbleSorter.Sort<Employee>(employees, Employee.Comparison_Name);//按名字排的方法
            public static bool Comparison_Name(Employee x, Employee y)
            {
                return x.name.CompareTo( y.name)<0;
            }
      

  7.   

    class Employee
        {
            private string name;
            private decimal salary;        public Employee(string name, decimal salary)
            {
                this.name = name;
                this.salary = salary;
            }        public override string ToString()
            {
                return string.Format("{0},{1:c}", name, salary);
            }        public static bool Comparison(object x, object y)
            {
                Employee e1 = (Employee)x;
                Employee e2 = (Employee)y;
                return e1.salary < e2.salary;
            }
        }    class BubbleSorter
        {
            static public void Sort(object[] sortArray)
            {
                for (int i = 0; i < sortArray.Length; i++)
                {
                    for (int j = i + 1; j < sortArray.Length; j++)
                    {
                        if (Employee.Comparison(sortArray[j], sortArray[i]))
                        {
                            object temp = sortArray[j];
                            sortArray[j] = sortArray[i];
                            sortArray[i] = temp;
                        }
                    }
                }
            }
        }