using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BubbleSort { delegate bool CompareOp(object lhs, object rhs); class BubbleSort { static public void Sort(object[] sortArray, CompareOp getMethod) { for(int i=0;i<sortArray.Length;i++) { for (int j = i + 1; j < sortArray.Length; j++) { if(getMethod(sortArray[i],sortArray[j])) { object temp=sortArray[i]; sortArray[i]=sortArray[j]; sortArray[j]=temp; } } } } } 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(name +",{0:C}", salary); } public static bool RhsIsCreater(object lhs, object rhs) { Employee empLhs = (Employee)lhs; Employee empRhs = (Employee)rhs; return (empRhs.salary > empLhs.salary) ? true : false; } } class Program { static void Main(string[] args) { Employee[] employees= { new Employee("John",10), new Employee("Tom",7), new Employee("Lucy",45), new Employee("Lily",20) }; CompareOp employeeCompareOp=new CompareOp(Employee.RhsIsCreater); BubbleSort.Sort(employees,employeeCompareOp); for(int i=0;i<employees.Length;i++) { Console.Write(employees[i].ToString()+"/n"); } Console.Read(); } } }
posted on 2010-08-03 21:33 java课程设计 阅读(155) 评论(0) 编辑 收藏 举报