// 委托的使用实例,排序举例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _13自定义排序
{
public delegate int MyCompareHandler(string s1, string s2);
class Program
{
static void Main(string[] args)
{
string[] arr = "100,20,21,120,1,9".Split(',');
MyCompareHandler cmp;
//cmp = string.Compare;
cmp = CompareByNumber;
for (int i = 0; i < arr.Length-1; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (cmp(arr[j], arr[j + 1]) > 0)
{
string t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
}
public static int CompareByNumber(string n1, string n2)
{
return Convert.ToInt32(n1) - Convert.ToInt32(n2);
}
}
}
6317

被折叠的 条评论
为什么被折叠?



