Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
.NET 5.0 breaking changes regarding `IEnumerable<T>.OrderBy` behavior on string comparison

I have below code

    public class Model
    {
        public int Id { get; set; }
        public string OrderNumber { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var models = new List<Model>
            {
                new Model {Id = 4, OrderNumber = "BT-3964-1"},
                new Model {Id = 2, OrderNumber = "BT3924"},
                new Model {Id = 1, OrderNumber = "bt3810v2"},
                new Model {Id = 5, OrderNumber = "BILL-TEST100"},
                new Model {Id = 3, OrderNumber = "BT-4887-Test3-Create"}
            };

            var reorderedModels = models.OrderBy(x => x.OrderNumber);

            Console.WriteLine("The sorted models are:");
            foreach (var model in reorderedModels)
            {
                Console.WriteLine($"OrderNumber: {model.OrderNumber}, Id: {model.Id}");
            }
        }
    }

In .NET Core 3.1 and 2.1, it gives below output

The sorted models are:
OrderNumber: BILL-TEST100, Id: 5
OrderNumber: bt3810v2, Id: 1
OrderNumber: BT3924, Id: 2
OrderNumber: BT-3964-1, Id: 4
OrderNumber: BT-4887-Test3-Create, Id: 3

But on .NET 5.0, it gives below output

The sorted models are:
OrderNumber: BILL-TEST100, Id: 5
OrderNumber: BT-3964-1, Id: 4
OrderNumber: BT-4887-Test3-Create, Id: 3
OrderNumber: bt3810v2, Id: 1
OrderNumber: BT3924, Id: 2

Why the breaking changes?

Additional Note: even using OrderBy(x => x.OrderNumber, StringComparer.InvariantCultureIgnoreCase), this issue still happens

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'm guessing it has something to do with the change to use International Components for Unicode (ICU) libraries as the default.

You can read about it specifically as it affects string comparison here: https://docs.microsoft.com/en-us/dotnet/standard/base-types/string-comparison-net-5-plus

An example given is for sorting strings. Their example is about instantiating a SortedtSet<string>, but the logic in the solution can be extended to cover your use of OrderBy by providing the explicit comparer you want to use.

The example:

//
// Potentially incorrect code - behavior might vary based on locale.
//
SortedSet<string> mySet = new SortedSet<string>();
List<string> list = GetListOfStrings();
list.Sort();

//
// Corrected code - uses ordinal sorting; doesn't vary by locale.
//
SortedSet<string> mySet = new SortedSet<string>(StringComparer.Ordinal);
List<string> list = GetListOfStrings();
list.Sort(StringComparer.Ordinal);
over 4 years ago · Santiago Trujillo Report

0

What @Loggar said and for your specific case to get consistent behaviour the change will be to pass the string comparer to OrderBy:

.OrderBy(x => x, StringComparer.InvariantCultureIgnoreCase)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!