SOLVED – How linq order by a Hyphen

Advertisement

A list contains a structure like that:

ColumnA

0+ABC
0-ABC
0001
0002
0003
0004

then sort the list using order by like list. OrdeBy(o=>o.ColumnA).ToList(), the below is the result:

0+ABC
0001
0002
0003
0004
0-ABC

So the question is why “0+ABC” at the top of the list but “0-ABC” is put to the end. it looks like linq sort “+” and “” using different methods.

You May Also Like: SOLVED – How To Get All Data From Last 5 Minutes in SQL

SOLUTION

It seems that the “Linguistic comparison” is used [https://docs.microsoft.com/en-us/dotnet/csharp/how-to/compare-strings#linguistic-comparisons].

Try adding “00-01” and “0-002” to your list and check the result.

To change the bahaviour, try, for example, list.OrderBy( o => o.ColumnA, CultureInfo.InvariantCulture.CompareInfo.GetStringComparer( CompareOptions.StringSort ) ).ToList( ).

You May Also Like: SOLVED – How To Update Connection Policy Of Synapse Dedicated SQL Pools

We hope the solutions in the above-mentioned article were helpful. Feel free to share your views with us.

Tagged with 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>