委托及人员employee排序

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     public class BubbleSorter
 9     {
10         public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison)
11         {
12             bool swapped = true;
13             do
14             {
15                 swapped = false;
16                 for (int i = 0; i < sortArray.Count-1; i++)
17                 {
18                     if(comparison(sortArray[i+1],sortArray[i]))
19                     {
20                         T temp = sortArray[i];
21                         sortArray[i] = sortArray[i + 1];
22                         sortArray[i + 1] = temp;
23                         swapped = true;
24                     }
25                 }
26             } while (swapped);
27         }
28     }
29     public class Employee
30     {
31         public Employee(string name, decimal salary)
32         {
33             this.Name = name;
34             this.Salary = salary;
35         }
36         public string Name { get; set; }
37         public decimal Salary { get; set; }
38         public override string ToString()
39         {
40             return string.Format("{0},{1:C}", Name, Salary);
41         }
42         public static bool CompareSalare(Employee el, Employee e2)
43         {
44             return el.Salary < e2.Salary;
45         }
46     }
47 }
48 static void Main(string[] args)
49         {
50             Employee[] employees = 
51             {
52                 new Employee("Bugs Bunny",20000),
53                 new Employee("Elmer Fudd",10000),
54                 new Employee("Daffy Duck",25000),
55                 new Employee("Wile Coyote",1000000.38m),
56                 new Employee("Foghorn Leghorn",23000),
57                 new Employee("RoadRunner",50000)
58             };
59             BubbleSorter.Sort(employees, Employee.CompareSalare);
60 
61             foreach (var employee in employees)
62             {
63                 Console.WriteLine(employee);
64             }
65 
66             Console.ReadKey();
67         }
posted @ 2012-11-07 11:44  转身就是一辈子  阅读(114)  评论(0)    收藏  举报