What should these comparisons be?

I have an application that uses string.compare (string, string) to sort some values. I can’t understand why β€œ1022” compares with less than β€œ10-23” and β€œ10-23” than less than β€œ1024”.

Is there a specific meaning of "-" that causes this result? Will string.compare overload the same result with different culture settings for the same data type (numbers with dashes)?

+3
source share
2 answers

From the documentation of string.Compare (String, String) :

Comparison is performed using word sorting rules.

and further :

.NET Framework : , . . , . , ( "-" ) , , "" "" . , , . - . Ordinal sort , Unicode .

: A & P , 9 (aka , ).

+5

, . , , StringComparison.Ordinal .

docs string.Compare, , :

. nonalphanumeric , . , ( "-" ) , "" "" .

, : "" - , - A < B, B < C C < ., . , " ". :

string s1 = "-0.67:-0.33:0.33";
string s2 = "0.67:-0.33:0.33";
string s3 = "-0.67:0.33:-0.33"; 
Console.WriteLine(s1.CompareTo(s2));
Console.WriteLine(s2.CompareTo(s3));
Console.WriteLine(s1.CompareTo(s3));

( 1,1, -1 )

+3

Source: https://habr.com/ru/post/1699149/


All Articles