"zzz" -le "~~~" False PS C:\temp> "~~~" -le "zzz" True ...">

Why is "zzz" -le "~~~" false?

As the title says, why is this happening?

PS C:\temp> "zzz" -le "~~~"
False

PS C:\temp> "~~~" -le "zzz"
True

"~" is the next ASCII character. I can not understand the sort where it comes to "z".

+4
source share
2 answers

Short answer: because it uses[String]::Compare() and returns the same result.

Longer answer: this is not a comparison of ASCII values. It depends on both the culture information and the comparison options, and the default is Word Sort.

, . , , , , .

. , , . System.Globalization.CompareOptions.

[string]::Compare('z','~')  
# 1

[string]::Compare('~','z')  
# -1

[string]::Compare('z','~',[cultureinfo]::CurrentCulture,[System.Globalization.CompareOptions]::Ordinal)  
# -4

[string]::Compare('~','z',[cultureinfo]::CurrentCulture,[System.Globalization.CompareOptions]::Ordinal)  
# 4
+5

, ~ () , - , . . , .

Try:

PS C:\> $x = @('aaa','~~~','zzz')
PS C:\> [System.Array]::Sort($x)
PS C:\> $x
~~~
aaa
zzz
PS C:\> [System.Array]::Sort($x,[System.StringComparer]::Ordinal)
PS C:\> $x
aaa
zzz
~~~

# .

en-US , :

PS C:\> $x = @("0","9","a","A","á","Á","ab","aB","Ab","áb","Áb","Æ","z","Z","~")
PS C:\> [Array]::Sort($x)
PS C:\> $x
~
0
9
a
A
á
Á
ab
aB
Ab
áb
Áb
Æ
z
Z
PS C:\> [Array]::Sort($x,[StringComparer]::Ordinal)
PS C:\> $x
0
9
A
Ab
Z
a
aB
ab
z
~
Á
Áb
Æ
á
áb

, ? , .NET Framework .

, [System.StringComparer]::CurrentCulture [System.StringComparer]::CurrentCultureIgnoreCase . , . :

PS C:\> [System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::InvariantCulture
PS C:\> [System.Array]::Sort($x)
PS C:\> $x
~~~
aaa
zzz
PS C:\> $x[0] -le $x[1]
True

, System.String.CompareOrdinal:

[System.String]::CompareOrdinal($StringA,$StringB)

, $StringA $StringB.
, $StringA $StringB.
, $StringA , $StringB.

, :

'zzz' -le '~~~'

:

PS C:\> [System.String]::CompareOrdinal('zzz','~~~') -le 0
True
+6

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


All Articles