public class Helper
{
public delegate bool Compare<T>(T t1,T t2);
public static void Sort<T>(T[] items, Compare<T> compare)
{
for (int i = 0; i < items.Length; i++)
{
for (int j = i + 1; j < items.Length; j++)
{
if (compare(items[i], items[j]))
{
var temp = items[i];
items[i] = items[j];
items[j] = (T)temp;
}
//compare > 则顺序,<则逆序 www.Rsion.com
}
}
}
}
public class Employee
{
public string Name { get; set; }
public decimal Salary { get; set; }
public static bool Compare(Employee a, Employee b)
{
return a.Salary < b.Salary;
}
public static bool CompareDesing(Employee a, Employee b)
{
return a.Salary > b.Salary;
}
}
public class Test
{
var employees =new Models.Employee[]{
new Models.Employee{Name="Jack",Salary=1000.20M},
new Models.Employee{Name="Smith",Salary=850.50M},
new Models.Employee{Name="Sonven",Salary=500},
new Models.Employee{Name="Abama",Salary=2500},
new Models.Employee{Name="John",Salary=1500}
};
Helper.Sort(employees, Models.Employee.CompareDesing);
}
{
public delegate bool Compare<T>(T t1,T t2);
public static void Sort<T>(T[] items, Compare<T> compare)
{
for (int i = 0; i < items.Length; i++)
{
for (int j = i + 1; j < items.Length; j++)
{
if (compare(items[i], items[j]))
{
var temp = items[i];
items[i] = items[j];
items[j] = (T)temp;
}
//compare > 则顺序,<则逆序 www.Rsion.com
}
}
}
}
public class Employee
{
public string Name { get; set; }
public decimal Salary { get; set; }
public static bool Compare(Employee a, Employee b)
{
return a.Salary < b.Salary;
}
public static bool CompareDesing(Employee a, Employee b)
{
return a.Salary > b.Salary;
}
}
public class Test
{
var employees =new Models.Employee[]{
new Models.Employee{Name="Jack",Salary=1000.20M},
new Models.Employee{Name="Smith",Salary=850.50M},
new Models.Employee{Name="Sonven",Salary=500},
new Models.Employee{Name="Abama",Salary=2500},
new Models.Employee{Name="John",Salary=1500}
};
Helper.Sort(employees, Models.Employee.CompareDesing);
}