I hope someone can prove me wrong here :)
If I do this:
List<string> a = new List<string> { "b", "c", "a", "aa" };
var b = a.OrderBy(o => o).ToList();
I would expect the result of 'b' to be:
a
aa
b
c
Instead, the result I get is:
a
b
c
aa
How can I get OrderBy to do a "correct" alphabetical sort? Am I just plain wrong? :)
You’re in the Danish culture, which treats aa
as å
and puts it after ø
accordingly. You can pass a string comparer that acts differently to OrderBy
to change that:
var b = a.OrderBy(o => o, StringComparer.InvariantCulture).ToList();
Most likely a cultural thing. You could try this:
List<string> a = new List<string> { "b", "c", "a", "aa" };
var b = a.OrderBy(o => o, StringComparer.InvariantCultureIgnoreCase).ToList();
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