Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting spaces between chars of two strings modify their order [duplicate]

Tags:

c#

sorting

I have 2 strings of the same length.
I was assuming (probably wrongly) that inserting a space between each character of each string will not change their order.

var e1 = "12*4";  
var e2 = "12-4";  
Console.WriteLine(String.Compare(e1,e2)); // -1 (e1 < e2)

var f1 = "1 2 * 4";  
var f2 = "1 2 - 4";  
Console.WriteLine(String.Compare(f1,f2)); // +1 (f1 > f2)

If I insert other characters (_ x for instance), the order is preserved.

What's going on ?

Thanks in advance.

like image 602
PhilippeC Avatar asked Jul 25 '17 14:07

PhilippeC


2 Answers

If you use Ordinal comparison, you will get the right result.

The reason is that ordinal comparison works by evaluating the numeric value of each of the chars in the string object, so inserting spaces will make no difference.

If you use other types of comparisons, there are other things involved. From the documentation:

An operation that uses word sort rules performs a culture-sensitive comparison wherein certain nonalphanumeric Unicode characters might have special weights assigned to them. Using word sort rules and the conventions of a specific culture, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list.

An operation that uses ordinal sort rules performs a comparison based on the numeric value (Unicode code point) of each Char in the string. An ordinal comparison is fast but culture-insensitive. When you use ordinal sort rules to sort strings that start with Unicode characters (U+), the string U+xxxx comes before the string U+yyyy if the value of xxxx is numerically less than yyyy.

like image 58
JuanR Avatar answered Oct 16 '22 20:10

JuanR


From MSDN:

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

When comparing strings, you should call the Compare(String, String, StringComparison) method, which requires that you explicitly specify the type of string comparison that the method uses.

It suggests that there is some cultural issue which means that the last space changes the sort order of the two.

like image 38
Rob Anthony Avatar answered Oct 16 '22 20:10

Rob Anthony