Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace delegatedemo1{class Program {static void Main (string[] args) {employee[] Emplo Yees = {New Employee ("ROLES_GRIS0", 4000), new employee ("Roles_gris1", 1000), New Employee ("Roles_gris2", 7000), new employee ("Roles_gris3"), New Employee ("Roles_gris 4 ", 9000), New employee (" ROLES_GRIS5 ", 8000), new employee (" ROLES_GRIS6 ", 8000), New Employee ("ROLES_GRIS7", 6000),}; Bubblesorter.sort (employees, employee.comparesalary); foreach (Var employee in employees) {Console.WriteLine (employee); } console.readkey (); }}///<summary>///bubble sort///</summary> class Bubblesorter {//<summary>///Bubble sort func<t, t,bool>: Encapsulates a method that has two parameters and returns the type value specified by the TResult parameter. </summary>//<typeparam name= "T" > What type of data to compare </typeparam>//<param name= "sorta Rry "> passed in the list an array of </param>//<param name=" comparison "> passed in the delegate instance is the method name </param> static public void sort<t> (ilist<t> sortarry, func<t, t,bool> comparison) {int j = 0; do {for (int i = j; i < sortarry.count-1; i++) {i F (comparison (Sortarry[i+1],sortarry[i])) {T tmp = sortarry[i]; Sortarry[i] = sortarry[i + 1]; Sortarry[i + 1] = tmp; }} j + +; } while (J < sortarry.count-1); }} class Employee {public string Name {get; set;} Public double Salary {get; set;} PubliC Employee (string name, double salary) {this. name = name; This. Salary = Salary; }//<summary>//rewrite string///</summary>//<returns></returns> public override string ToString () {return string. Format ("{0},{1:c}", Name, Salary); }///<summary>///Salary comparison function///</summary>//<param name= "E1" ></param> ; <param name= "E2" ></param>///<returns></returns> public static bool Comparesala RY (employee E1, employee E2) {return e1. Salary < E2. Salary; } }}
Bubble sort (delegate) of C #