Home > Software engineering >  C# how to sort user input from textbox in ascending order
C# how to sort user input from textbox in ascending order

Time:09-29

im trying to allow a user to enter ten alphanumeric values into 10 textboxes in ascending order. 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.im not sure how I can implement this.

the code below is my list of strings that displays 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 textBox1_TextChanged(object sender, EventArgs e)
    {

    }

here is my listbox that generates random strings and my textboxes for the user to enter them in acending order

CodePudding user response:

You can use OrderBy by to sort the list item and use SequenceEqual to compare two list order.

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());            
}

CodePudding user response:

Why do not use .Sort() ?

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

and after place your listBox1

CodePudding user response:

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

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)!

  • Related