I am trying to create a routine in C# that sorts a list added to a Multi-Line text box. Once that is done, there is an option to remove all blank lines. Can someone tell me how I would go about doing this? here is what I have so far, but it doesn't work at all when I select the box and click sort:
private void button1_Click(object sender, EventArgs e)
{
    char[] delimiterChars = { ',',' ',':','|','\n' };
    List<string> sortBox1 = new List<string>(textBox2.Text.Split(delimiterChars));
    if (checkBox3.Checked) //REMOVE BLANK LINES FROM LIST
    {
        sortBox1.RemoveAll(item => item == "\r\n");
    }
    textBox3.Text = string.Join("\r\n", sortBox1);
}
                If you're splitting the string on '\n', sortBox1 won't contain a string containing \n. I would just use String.IsNullOrWhiteSpace, though:
sortBox1.RemoveAll(string.IsNullOrWhiteSpace);
                        You forgot to sort the lines:
sortBox1.Sort();
A blank line is not "\r\n", that is a line break. Blank lines are empty strings:
sortBox1.RemoveAll(item => item.Length == 0);
You can also remove the blank lines when splitting the string:
private void button1_Click(object sender, EventArgs e) {
    char[] delimiterChars = { ',',' ',':','|','\n' };
    StringSplitOptions options;
    if (checkBox3.Checked) {
        options = StringSplitOptions.RemoveEmptyEntries;
    } else {
        options = StringSplitOptions.None;
    }
    List<string> sortBox1 = new List<string>(textBox2.Text.Split(delimiterChars, options));
    sortBox1.Sort();
    textBox3.Text = string.Join("\r\n", sortBox1);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With