需要引用:using System.Linq;
static void Main(string[] args)
{
#region
Console.WriteLine("请输入您要排序的数字,用逗号隔开,输入完后按回车键查看结果:");
string numStr = Console.ReadLine();
// 把你输入的数字去掉俩端可能存在的空格,然后按照逗号分隔成数组并使用linq语法转换为double类型的list
ListnumArr = new List (numStr.Trim().Split(',').Select(x => double.Parse(x)));
numArr.Sort();// 排序
string result = "";
for (int i = numArr.Count - 1; i >= 0; i--)// 从大到小输出
{
result += numArr[i] + ", ";
}
Console.WriteLine(result);
#endregion
Console.ReadLine();
}
运行结果:
我也来掺和一下,页面源码》此处 onkeyup和onafterpaste 是让文本框只准输入数字
a:
b:
c:
后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
if (a1.Text != "" && b1.Text != "" && c1.Text != "") //判断是否为空
{
int a = int.Parse(a1.Text);//string 转 int
int b = int.Parse(b1.Text);
int c = int.Parse(c1.Text);
int x;
if (a < c) //a c对比 假如c大 将最大值赋值给a
{
x = a;
a = c;
c = x;
}
if (a < b)//a b对比 假如b大 将最大值赋值给a
{
x = a;
a = b;
b = x;
}
if (b < c)//b c对比 假如c大 将最大值赋值给b
{
x = b;
b = c;
c = x;
}
Response.Write(a+"--"+b+"--"+c);
}
else
{ Response.Write("不能为空");}
}
附上结果:
//数组排序,nums参数为要排序的数组,例如int[] nums={1,2,3};
public static void NumSort(int[] nums)
{
for (int i = 0; i < nums.Length - 1; i++)
{
for (int j = i+1; j < nums.Length ; j++)
{
if (nums[i] < nums[j])
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
Console.WriteLine("从大到小顺序排序后结果如下:");
for (int i = 0; i < nums.Length; i++)
{
Console.Write(nums[i] + " ");
}
}
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
string[] arr = new string []{"b","c","a"};//定义数组
Array.Sort(arr);//排序
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);//输出
}
}
}
}