floormind
floormind

Reputation: 2028

c# splitting an array and displaying it in descending order in a textbox

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

Upvotes: 1

Views: 1534

Answers (3)

Mark
Mark

Reputation: 3283

this should work:

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

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

Upvotes: 1

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

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.

Upvotes: 1

tukaef
tukaef

Reputation: 9214

int[] newOne = arrText.Select(x => int.Parse(x)).OrderByDescending(x => x).ToArray();

Upvotes: 3

Related Questions