Why do Linq's OrderBy not always adhere to CurrentCulture?

Viewed 110

I have a problem where doing an OrderBy on a List<object> containing strings is returning different results in different environments, even though the CurrentCulture is set the same.

Example:

Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
var people = new List<object>{"Åke", "Adam", "Bengan"};
return people.OrderBy(p => p);

The names are in Swedish and should be ordered:

  1. Adam
  2. Bengan
  3. Åke

This example is very much simplified, and I need to use a generic data type object for the list.

The code is run inside a .NET 4.8 WCF-service, and I've tried enforcing the culture in many different ways. For now on my machine (Win 10) the WCF service returns US-sort: 1. Adam 2. Åke 3. Bengan, but when I write a unit test, the code returns the correct Swedish-sort. I also have a couple of external test-servers where the result differ, some return the correct Swedish sort from the WCF-service and others not. I need a way of strictly enforcing the culture for sorting.

What more could impact the way of sorting? I have read up on SortVersion but can not find out how to apply it to my example.

Cheers!

UPDATE 1

When running: RuntimeInformation.FrameworkDescription both the unit test and WCF service returns: .NET Framework 4.8.4420.0.

Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
Comparer<object>.Default.Compare("Åke", "Bengan");

The unit test returns 1 (which is correct), the WCF service returns: -1.

UPDATE 2

Regarding versions. All machines have .NET Framework 4.8 installed.

OrderBy with Swedish letters works on this version:

  • Windows Server 2016 Standard

But doesn't work on:

  • Windows 10 Enterprise version: 21H2
  • Windows Server 2019 version: 2004

We have no other machines to reproduce this bug on at this time. There must be a way to enforce the culture specific sort on all machines?

0 Answers
Related