static void Main(string[] args)
{
Console.WriteLine("请输入一组数(空格分隔):");
string str = Console.ReadLine();
string[] sarr = str.Split(' ');
Sort(sarr);
}
public static void Sort<T>(T[] str) where T : IComparable // where约束 只能代表某些类型
{
T[] arr = new T[str.Length];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = str[i];
}
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1 - i; j++)
{
if (arr[j].CompareTo(arr[j + 1]) > 0)
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
foreach (var item in arr)
{
Console.Write(item + " ");
}
Console.ReadKey();
}