Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
.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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda