Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

243
Vistas
.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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda