I need to replicate the exact order produced by another program that used the ORDER BY clause in its SQL query:
select Name from TABLE_NAME ORDER BY Name
Result:
To reproduce the order on my side, I am using C# net8.0.
Here is the code I used:
var sorted = list.OrderBy(o => o.Name);
Result:
I tried with StringComparer.Ordinal:
var sorted = list.OrderBy(o => o.Name, StringComparer.Ordinal);
Result:
I also tried using other StringComparer options without success.
Is there any way to achieve the same behavior as SQL?
2
The sorting behavior you are seeing on your C# side is consistent with standard ASCII case sensitive behavior, since C
comes before [
which comes before a
. On the SQL side, it appears that a case insensitive collation is being used, and that sorting is happening as if every character had been lowercased. We know that because there a
is sorting before C
, implying the latter is sorting as c
.
You could try sorting the lowercased character on the C# side:
var sorted = list.OrderBy(o => o.Name.ToLower(), StringComparer.Ordinal);
1