1

i just wrote a code that will sort the textbox but i want to know is there any other ways that does not require an intermediate array?!(just to improve performance)

here's the code i wrote:

        string[] textbox = new string[textBox1.Lines.Length];
        textbox = textBox1.Text.Split('\r');
        textBox1.Clear();
        Array.Sort(textbox);
        foreach(string text in textbox)
        {
            textBox1.AppendText(text + "\r\n");
        }

2 Answers 2

1

You can work directly with the Lines property (and this is already an array).

 textBox1.Lines = textBox1.Lines.OrderBy(l => l).ToArray();

(Note that in any case you have to rebuild the Lines array so it is not really possible to avoid an array, it is just materialized after the OrderBy in this code)

Sign up to request clarification or add additional context in comments.

2 Comments

this was way more faster than what i wrote.
the appendtext takes too much time to perform the action
0

Use OrderBy:

var s= textBox1.Text.Split('\r').OrderBy(c=>c);
textBox1.Clear();
foreach (string text in s)
{
    textBox1.AppendText(text + "\r\n");
}

You could also do this without foreach loop just by using String.Join method:

var s = string.Join("\r\n", textBox1.Text.Split('\r').OrderBy(c => c));
textBox1.Clear();
textBox1.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.