PhilippeC PhilippeC - 3 years ago 82
C# Question

Inserting spaces between chars of two strings modify their order

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download