using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace ConsoleApp1{ class Program { ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//聲明一個類
People[] arrays = new People[5];
arrays[0] = new People("p1", 3);
arrays[1] = new People("p1",321);
arrays[2] = new People("p1", 12);
arrays[3] = new People("p1", 3231);
arrays[4] = new People("p1", 21);
//調用冒泡方法
SortMethod(arrays,People.ComPare);
//輸出調用後
foreach (People P in arrays)
{
Console.WriteLine(P);
}
Console.ReadKey();
}
//定義冒泡方法
static bool isChange = false;
public static void SortMethod(People[] arrays,Func<int,int,bool> compare)
{
do
{
isChange = false;
for (int i = 0; i < arrays.Length - 1; i++)
{
if (compare(arrays[i].salary,arrays[i+1].salary))
{
People temp = arrays[i];
arrays[i] = arrays[i + 1];
arrays[i + 1] = temp;
isChange = true;
}
}
}
while (isChange);
}
}
}
下麵是一個PeoPle類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class People { private string name; public int salary; public People(string name, int salary) { this.name = name; this.salary = salary; } public static bool ComPare(int salary1, int salary2) { if (salary1 > salary2) return true; return false; } //重寫 public override string ToString() { return name +","+salary.ToString(); } } }