简体   繁体   English

C# 如何按升序对文本框中的用户输入进行排序

[英]C# how to sort user input from textbox in ascending order

I'm trying to allow a user to enter ten alphanumeric values into 10 textboxes in ascending order.我试图让用户按升序在 10 个文本框中输入十个字母数字值。 Once the values are entered I want to use any sorting algorithm to check if the values have been sorted correctly so I can display a message to the user that their ordering was either correct or incorrect.输入值后,我想使用任何排序算法来检查值是否已正确排序,以便我可以向用户显示一条消息,说明他们的排序是正确的还是不正确的。 I'm not sure how I can implement this.我不确定如何实现它。

the code below is my list of strings that display in random to the user下面的代码是我随机显示给用户的字符串列表

 private void button1_Click(object sender, EventArgs e)
    {


        listBox1.Items.Clear();
        var list = new List<string> { "12fe", "46ge", "7uf", "15gs", "64ku", "42nt", "04bv", "07nh", "03lf", "86nj" };
        var random = new Random();

        for (var i = 0; i < 10; i++)
        {
           
            int index = random.Next(list.Count);
            listBox1.Items.Add(list[index]);
        }

private void button2_Click(object sender, EventArgs e) { private void button2_Click(object sender, EventArgs e) {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

here is my Listbox that generates random strings and my textboxes for the user to enter them in ascending order这是我的列表框,它生成随机字符串和我的文本框,供用户按升序输入

You can use OrderBy by to sort the list item and use SequenceEqual to compare two list order.您可以使用OrderBy by 对列表项进行排序,并使用SequenceEqual来比较两个列表顺序。

private void button2_Click(object sender, EventArgs e)
{
    var list = new List<string> { "12fe", "46ge", "7uf", "15gs", "64ku", "42nt", "04bv", "07nh", "03lf", "86nj"};
    var orderedList = list.OrderBy(i => i);
    MessageBox.Show(orderedList.SequenceEqual(list).ToString());            
}

Why do not use.Sort()?为什么不使用.Sort()?

var list = new List<string> { "12fe", "46ge", "7uf", "15gs", "64ku", "42nt", "04bv", "07nh", "03lf", "86nj" };
list.Sort();

and after place your listBox1然后放置您的 listBox1

It is possible to add a special button to check the order;可以添加一个特殊按钮来检查订单; This is what I did and this is the result where the order is compared and shown in the MessageBox as you indicated in your question with icons added for greater clarity by the MessageBox rating这就是我所做的,这是比较订单并显示在 MessageBox 中的结果,正如您在问题中指出的那样,并通过 MessageBox 评级添加了图标以提高清晰度

I did a program to make sure and this is the picture: The result of program to check ascending order我做了一个程序来确保这是图片:检查升序程序的结果

And this is the code I used to read the contents of list box and then compare from the list to check if it is ascending order这是我用来读取列表框内容然后从列表中比较以检查它是否是升序的代码

        private void sort_Click(object sender, EventArgs e)
    {
        List<string> list = new List<string> { };

        foreach (string temp in listBox1.Items)
        {
            list.Add(temp);
        }
        var ascending_order = list.OrderBy(item => item);
        if (ascending_order.SequenceEqual(list))
        {
            MessageBox.Show("The ordering of the list is correct", "Sort result",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("The ordering of the list is Incorrect", "Sort result", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

This is my first answer on this site (stackoverflow)!这是我在这个网站(stackoverflow)上的第一个答案!

In your sort button click, take all the 10 texts and put them in an array在您的排序按钮中单击,获取所有 10 个文本并将它们放入一个数组中

var a = new [] {
  textbox1.Text,
  textbox2.Text,
  textbox3.Text,
  ...
  textbox10.Text
};

Now you can sort them using some common LINQ sorting:现在您可以使用一些常见的 LINQ 排序对它们进行排序:

var sorted = a.ToList().Sort();

Now you can cehck if they're in the same order with sequenceequal:现在,如果它们与 sequenceequal 的顺序相同,您可以 cehck:

var isSame = sorted.SequenceEquals(a);

If this is an academic exercise and you're not expected to use things like LINQ, i suggest you look at making your own sort (bubblesort is an easy algo to write) and your own version of sequenceEqual (use a for loop to step through the two lists checking if the elements are the same)如果这是一个学术练习并且你不希望使用像 LINQ 这样的东西,我建议你看看自己的排序(冒泡排序是一个容易编写的算法)和你自己的 sequenceEqual 版本(使用 for 循环来单步执行两个列表检查元素是否相同)

Bear in mind that by default in C# sorting is case sensitive请记住,默认情况下 C# 排序区分大小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM