Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected Linq OrderBy

Tags:

c#

linq

I have the following test which is failing:

[TestCase]
public void Should_order_numeric_suffix()
{
    var names = new List<string>
                    {
                        "Buisness Unit 7",
                        "Business Unit 1",
                        "Buisness Unit 3",
                        "Business Unit 6",
                        "Business Unit 4",
                        "Buisness Unit 2",
                        "Business Unit 5"
                    };

    List<string> ordered = names.OrderBy(x => x).ToList();

    Assert.That(ordered[0], Is.EqualTo("Business Unit 1"));
}

The actual order of the ordered List is:

Business Unit 2
Business Unit 3
Business Unit 7
Business Unit 1
Business Unit 4
Business Unit 5
Business Unit 6

Obviously it is ordering alphabetically correctly but I expected the numeric suffix to be ordered also.

like image 265
dagda1 Avatar asked Mar 18 '26 21:03

dagda1


1 Answers

You have typos in your list, some entries spell "Buisness" instead of "Business"... and "Buisness" comes before "Business" in alphabetical order

like image 64
Thomas Levesque Avatar answered Mar 20 '26 10:03

Thomas Levesque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!