简体   繁体   中英

.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

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);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM