1

I wish to display an array of text in a textbox, after splitting it with a comma, i pass a bunch of numbers into textbox1 and split it with the commas, how do i sort the number in descending order. here is the method i got so far

    private void btnCalc_Click(object sender, EventArgs e)
    {
        //string A = txtInput.Text;
        string[] arrText = txtInput.Text.Split(',');
        int[] newOne = new int[]{};
        foreach (string r in arrText)
        {

        }
        txtOutput.AppendText( );
    }

3 Answers 3

3
int[] newOne = arrText.Select(x => int.Parse(x)).OrderByDescending(x => x).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to do it like this:

private void btnCalc_Click(object sender, EventArgs e)
{
    //string A = txtInput.Text;
    string[] arrText = txtInput.Text.Split(',');
    txtOutput.Text = string.Join(",",arrText.Select( s => int.Parse(s)).OrderByDescending( i => i))
}

Note that this will blow up if some of the input text between the commas is not a number.

Comments

1

this should work:

var newOne = arrText.OrderByDescending(int.Parse).ToArray();

foreach (var s in newOne)
{
      txtOutput.AppendText(s);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.