C#中数字的比较和排序

来源:百度知道 编辑:UC知道 时间:2024/06/30 04:14:00
要求 TextBox :用户输入一串数字, 用","分隔, 如:3.1,2.92,4.5…
只允许输入数字、小数点和逗号
要求Buttons:
点击“显示最大值”:弹出消息框显示"最大值是X"
点击“显示最小值“:弹出消息框显示"最小值是X"
排序:点“从大到小 "或"从小到大",将TextBox中的数字重新排序显示
使用接口 IComparer.Comparer()实现“从大到小 ”排序
求代码......

这么写就行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string text = "13.3,12,41.5,123,342,12.6";
string[] strArr = text.Split(new char[] { ','});
float[] floatArr = Array.ConvertAll<string, float>(strArr, new Converter<string, float>(StringToFloat));
Array.Sort(floatArr);
string min = floatArr[0].ToString();//最小
string nax = floatArr[floatArr.Length - 1].ToString();//最大
foreach (float var in floa