I see very strange sorting behavior using CaseInsensitiveComparer.DefaultInvariant. Words that begin with a leading hyphen "-" end with a sort, as if the hyphen was not there, but not sorted before the actual letters, which happens with other punctuation.
So, given {"hello", ".net", "-less"}, I get {".net", "hello", "-less"} instead of the expected {"-less", ".net", " hello "}.
Or, formulated as a test case:
[TestMethod] public void TestMethod1() { var rg = new String[] { "x", "z", "y", "-less", ".net", "- more", "a", "b" }; Array.Sort(rg, CaseInsensitiveComparer.DefaultInvariant); Assert.AreEqual( "- more,-less,.net,a,b,x,y,z", String.Join(",", rg) ); }
... which looks like this:
Assert.AreEqual failed. Expected:<- more,-less,.net,a,b,x,y,z>. Actual: <- more,.net,a,b,-less,x,y,z>.
Any ideas what is going on?
Edit:
It seems like, by default, .NET comes up with things when sorting strings that cause leading hyphens to sort in weird places so that co-ops and co-ops sort together. So, if you want your leading hyphen words to end and the beginning with a different punctuation, you should say this not so:
Array.Sort(rg, (a, b) => String.CompareOrdinal(a, b));