s) .ToArray (...">

Why "1.0" <"-1.0" <"1.1"?

Consider this piece of code:

var sorted = new[] { "-1.0", "0.0", "1.0", "1.1", "2.0" }
    .OrderBy (s => s)
    .ToArray ();
Console.WriteLine (string.Join (", ", sorted));

      

On my system, this prints

0.0, 1.0, -1.0, 1.1, 2.0

      

Considering the Ascii code is -

smaller than the ascii codes of the numbers I was expecting -1.0, 0.0, 1.0, 1.1, 2.0

.

It definitely confuses me why -1.0

is between 1.0

and 1.1

. These two start with the same char, so everything in between must start with a character 1

.

I vaguely suspect culture or language setting influences this, but mine (a mixture of some German and many English) probably shouldn't be any different from English or invariant in the above case.

+3


source share


1 answer


According to MSDN docs String.Compare :

Notes to Callers: Character sets include unresponsive characters. The Compare (String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method does not consider these characters when it performs a linguistic or culture-sensitive comparison. To mark ignorant characters in your comparison, set CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for options.

If you add StringComparer.Ordinal

it will work as expected.

        var sorted = new[] { "-1.0", "0.0", "1.0", "1.1", "2.0" }
            .OrderBy(s => s, StringComparer.Ordinal)
            .ToArray();
        Console.WriteLine(string.Join(", ", sorted));

      



So you can see that -

will be completely ignored, which means that "-1.0"

u "1.0"

are the same

Same as MSDN CompareOptions Remakrs Enumeration

The .NET Framework uses three different sorting methods: word sort, string sort, and order sort. Sort Word performs culture-sensitive string comparisons. Some non-alpha numeric characters may have special weights assigned to them. For example, a hyphen ("-") may have very little weight assigned to it so that "coop" and "coop" appear next to each other in a sorted list. String sorting is similar to collocation, except that there are no special cases. Therefore, all nonalphanumeric characters come before all alphanumeric characters. Ordinal sort compares strings based on the Unicode values โ€‹โ€‹of each element in the string.

+8


source







All Articles