Explore how to handle sorting by delegate

  • 2020-05-17 06:11:48
  • OfStack


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class  Bubble sort 
    {
        // The first thing to understand is bubble sorting , It's really just a comparison of the front and the back of the index , If it's bigger than this 2 The position of the values will be reversed 
        static void Main()
        {
            int[] str ={ 0, 14, 3, 6, 1, 30, 10, 9, 28 };
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    if (str[j] < str[i])
                    {
                        int index = str[i];
                        str[i] = str[j];
                        str[j] = index;
                    }
                }
            }
            for (int m = 0; m < str.Length; m++)
            {
                Console.WriteLine(str[m]);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    public delegate bool DelegateTest(object obj1, object obj2);
    class Class1
    {
        static void Main()
        {
            Employee[] Employees =
              { 
                  new Employee("huguo",1000000),
                  new Employee("lili",20000),
                  new Employee("lulu",30000),
                  new Employee("xixi",50000),
                  new Employee("jianjian",10000),
                  new Employee("yoyo",9000)
              };
            // entrust DelegateTest The proxy method is Greate
            DelegateTest MyTest = new DelegateTest(Employee.Greate);
            Sorter MySort = new Sorter();
            // Bubble algorithm in the first 1 Three parameters are corresponding Employees An array of information , The first 2 Two parameters are the delegate 
            MySort.Sort(Employees, MyTest);
            for (int m = 0; m < Employees.Length; m++)
            {
                Console.WriteLine(Employees[m].ToString());
            }
        }
    }
    class Employee
    {
        public string Name;
        public int Salary;
        public Employee(string Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;
        }
        // with override rewrite string methods 
        public override string ToString()
        {
            return string.Format(Name + ",{0:C},", Salary);
        }
        // define 1 A method of , if obj2 To get the  Salary Is greater than obj1 It returns true;
        public static bool Greate(object obj1, object obj2)
        {
            Employee Employee1 = (Employee)obj1;
            Employee Employee2 = (Employee)obj2;
            return (Employee2.Salary > Employee1.Salary) ? true : false;
        }
    }
    class Sorter
    {
        public void Sort(object[] ArrayObj, DelegateTest Test)
        {
            // So here's the bubbling algorithm 
            for (int i = 0; i < ArrayObj.Length; i++)
            {
                for (int j = i + 1; j < ArrayObj.Length; j++)
                {
                    if (Test(ArrayObj[j], ArrayObj[i]))
                    {
                        object Temp = ArrayObj[i];
                        ArrayObj[i] = ArrayObj[j];
                        ArrayObj[j] = Temp;
                    }
                }
            }
        }
    }
}


Related articles: