111,108
社区成员




public delegate bool Contrast<T>(T t1, T t2);
public static void Sequence<T>(T[] items, Contrast<T> contrast)
{
for (int i = 0; i < items.Length; i++)
{
for (int j = i + 1; j < items.Length; j++)
{
if (contrast(items[i], items[j]))
{
var temp = items[i];
items[i] = items[j];
items[j] = (T)temp;
}
}
}
}
//调用方法
string[] arr = { "zg", "bb", "A", "ws", "22", "8" ,"09"};
Sequence(arr, (a, b) => a[0] > b[0]);//顺序
//Sequence(arr, (a, b) => a[0] < b[0]);//倒序
Console.WriteLine(string.Join(",", arr));
using System;
public class Test
{
public static void Main()
{
string[] data = { "g", "b", "q", "w", "2", "3" };
for (int i = 0; i < data.GetLength(0); i++)
{
for (int j = 1; j < data.GetLength(0) - i; j++)
{
if (data[j].CompareTo(data[j - 1]) < 0)
{
string t = data[j];
data[j] = data[j - 1];
data[j - 1] = t;
}
}
}
foreach (string s in data) Console.WriteLine(s);
}
}
http://ideone.com/AyxLdQ
在线测试通过Array.Sort(arr);
string[] arr = { "zg", "bb", "A", "ws", "22", "8" ,"09"};
string temp = string.Empty;
//冒泡排序
for (int i = arr.Length; i > 0; i--)
{
for (int j = 0; j < i - 1; j++)
{
//这里对比的就是元素首字符的ascii码大小
if (arr[j][0] > arr[j + 1][0])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
Console.WriteLine(string.Join(",", arr));