You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sort-Object, unlike operators, uses the current culture and doesn't support requesting the invariant culture via -Culture #18655
However, even with the de-facto discrepancy, there is no currently no way to opt-into using the invariant culture, given that the -Culture parameter is [string]-typed and that the name of the invariant culture is the empty string (''), and passing the empty string appears to be ignored .
Update: There is a workaround: using the LCID value of the Invariant Culture, 127 (note that the majority of cultures no longer have unique LCID values and share the catch-all value 4096, but the invariant culture's LCID is unique):
'i','I'|Sort-Object-CaseSensitive -Culture 127# OK
Expected behavior
Ii---iI
That is, in the invariant culture i should sort beforeI
Actual behavior
Ii---Ii
That is, the -Culture argument was effectively ignored.
Oh Lordy, current vs invariant again. Michael Klement (@mklement0) this looks like it comes from your checking things for #18638 (that answer should really go in a KB article somewhere, it's wasted as a github response, but that's another matter).
So what we're saying is if the culture sort order doesn't match the invariant -gt / -lt and sort-object diverge.
PS> [cultureinfo]::CurrentCulture = 'no'
PS> "c" -gt "b"
True
PS> "Å" -gt "b"
False
PS> "c" , "Å" , "b" | sort-object
b
c
Å
(I got a lecture about Å from a Norwegian client 10 years or so ago, so I keep using it).
I would class this divergence as a bug
I am trying to think of a case where someone would set the machine's culture and want comparison operators to ignore it. I have clients who keep all their servers in one time zone, regardless of location, or keep all servers set to US culture etc. There should at least be an "opt-in" for compare using current culture (if there is a change it's possible breaking change, so there needs to be an opt out, whether in or out is the default is debatable)
I can also see a situation where someone would spot the difference in sort orders for an international company and specify scripts use a particular language, and invariant culture should work for this. Converting a culture object to a string and not accepting empty string looks like a second bug to me. The assumption that invariant = US holds for most things (not time format), so the work round is easy. But any culture parameter should process strings and culture objects correctly.
It is certainly desirable for all operators and cmdlets to consistently default to the invariant culture.
Converting a culture object to a string and not accepting empty string looks like a second bug to me.
Indeed, but this can easily be fixed.
Looking at the code, I realize that there are at least workarounds for now: you may pass 127 or 0x7f to -Culture and have it recognized as the invariant culture, via its LCID (Language Code Identifier):
Given the above, you can't help but wonder whether object would have been the appropriate parameter type, so that names (strings), LCIDs, and [cultureinfo] instances can be detected as such.
As you can see from the above, an argument (that can't be parsed as a number) is passed on to the [cultureinfo]::new([string] $name) constructor, which - with respect to predefined culture-info objects - only accepts their .Name property value (case-insensitively), such as 'en-US' for a specific culture, and 'en' for a neutral culture.
The problem, however, is that - hypothetical - names of non-predefined cultures are also accepted, which will typically amount to quiet failure, namely if you have a typo in your -Culture argument.
E.g., the following returns an ad hoc [cultureinfo] instance with the given, fictitious name; its other properties appear to be the same as that of [cultureinfo]::InvariantCulture:
[cultureinfo]::new('foo-bar')
This could be helped by using the following method instead, which, however, is at least technically a breaking change (though existing code that relies on it by definition never worked as intended - unless a bogus name was deliberately chosen to in effect get the invariant culture):
// Failing sample call from PowerShell:// [cultureinfo]::GetCultureInfo('foo-bar', $true)staticcultureinfoGetCultureInfo(stringname,boolpredefinedOnly)
That, along with changing the -Culture parameter to object (so that preexisting, possibly custom [cultureinfo] instances can be accepted), would rule out quiet failures.
Each language identifier is composed of a primary language identifier indicating the language and a sublanguage identifier indicating the country/region.
microsoft-github-policy-service commented on Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 28, 2023
Activity
237dmitry commentedon Nov 24, 2022
What if we use
TwoLetterISOLanguageName,ThreeLetterISOLanguageNameorThreeLetterWindowsLanguageNamestandards instead ofName?jhoneill commentedon Nov 24, 2022
Oh Lordy, current vs invariant again.
Michael Klement (@mklement0) this looks like it comes from your checking things for #18638 (that answer should really go in a KB article somewhere, it's wasted as a github response, but that's another matter).
So what we're saying is if the culture sort order doesn't match the invariant
-gt/-ltandsort-objectdiverge.(I got a lecture about Å from a Norwegian client 10 years or so ago, so I keep using it).
I would class this divergence as a bug
I am trying to think of a case where someone would set the machine's culture and want comparison operators to ignore it. I have clients who keep all their servers in one time zone, regardless of location, or keep all servers set to US culture etc. There should at least be an "opt-in" for compare using current culture (if there is a change it's possible breaking change, so there needs to be an opt out, whether in or out is the default is debatable)
I can also see a situation where someone would spot the difference in sort orders for an international company and specify scripts use a particular language, and invariant culture should work for this. Converting a culture object to a string and not accepting empty string looks like a second bug to me. The assumption that invariant = US holds for most things (not time format), so the work round is easy. But any culture parameter should process strings and culture objects correctly.
mklement0 commentedon Nov 24, 2022
Thanks, James O'Neill (@jhoneill).
I agree, but in the past a similar divergence was retained for fear of breaking backward compatibility:
It is certainly desirable for all operators and cmdlets to consistently default to the invariant culture.
Indeed, but this can easily be fixed.
Looking at the code, I realize that there are at least workarounds for now: you may pass
127or0x7fto-Cultureand have it recognized as the invariant culture, via its LCID (Language Code Identifier):PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/OrderObjectBase.cs
Lines 65 to 94 in cba9dac
Given the above, you can't help but wonder whether
objectwould have been the appropriate parameter type, so that names (strings), LCIDs, and[cultureinfo]instances can be detected as such.It's worth noting, however, that LCIDs are deprecated: see https://learn.microsoft.com/en-us/windows/win32/intl/language-identifier-constants-and-strings
237dmitry
As you can see from the above, an argument (that can't be parsed as a number) is passed on to the
[cultureinfo]::new([string] $name)constructor, which - with respect to predefined culture-info objects - only accepts their.Nameproperty value (case-insensitively), such as'en-US'for a specific culture, and'en'for a neutral culture.The problem, however, is that - hypothetical - names of non-predefined cultures are also accepted, which will typically amount to quiet failure, namely if you have a typo in your
-Cultureargument.E.g., the following returns an ad hoc
[cultureinfo]instance with the given, fictitious name; its other properties appear to be the same as that of[cultureinfo]::InvariantCulture:This could be helped by using the following method instead, which, however, is at least technically a breaking change (though existing code that relies on it by definition never worked as intended - unless a bogus name was deliberately chosen to in effect get the invariant culture):
That, along with changing the
-Cultureparameter toobject(so that preexisting, possibly custom[cultureinfo]instances can be accepted), would rule out quiet failures.microsoft-github-policy-service commentedon Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 15, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 28, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.