Bubble sort (delegate) of C #

Source: Internet
Author: User

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&gt        ; <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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.