Skip to content

Sort-Object, unlike operators, uses the current culture and doesn't support requesting the invariant culture via -Culture #18655

Closed

Description

Prerequisites

Steps to reproduce

Unlike PowerShell's operators, which use the invariant culture for comparisons, Sort-Object seems to use the current culture.

This unfortunate discrepancy between operators and cmdlets is nothing new:

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 .

[cultureinfo]::CurrentCulture = 'tr'

'i', 'I' | Sort-Object -CaseSensitive

'---'

'i', 'I' | Sort-Object -Culture ([cultureinfo]::InvariantCulture) -CaseSensitive

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

I
i
---
i
I

That is, in the invariant culture i should sort before I

Actual behavior

I
i
---
I
i

That is, the -Culture argument was effectively ignored.

Error details

No response

Environment data

PowerShell Core 7.3.0

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 24, 2022
  2. 237dmitry commented on Nov 24, 2022

    @237dmitry

    What if we use TwoLetterISOLanguageName, ThreeLetterISOLanguageName or ThreeLetterWindowsLanguageName standards instead of Name?

    'I', 'i' | Sort-Object -CaseSensitive -Culture tr
    '---'                                            
    'I', 'i' | Sort-Object -CaseSensitive -Culture iv
  3. jhoneill commented on Nov 24, 2022

    @jhoneill

    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.

  4. mklement0 commented on Nov 24, 2022

    @mklement0
    ContributorAuthor

    Thanks, James O'Neill (@jhoneill).

    I would class this divergence as a bug

    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.

    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):

    set
    {
    if (string.IsNullOrEmpty(value))
    {
    _cultureInfo = null;
    return;
    }
    int cultureNumber;
    string trimmedValue = value.Trim();
    if (trimmedValue.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
    {
    if ((trimmedValue.Length > 2) &&
    int.TryParse(trimmedValue.AsSpan(2), NumberStyles.AllowHexSpecifier,
    CultureInfo.CurrentCulture, out cultureNumber))
    {
    _cultureInfo = new CultureInfo(cultureNumber);
    return;
    }
    }
    else if (int.TryParse(trimmedValue, NumberStyles.AllowThousands,
    CultureInfo.CurrentCulture, out cultureNumber))
    {
    _cultureInfo = new CultureInfo(cultureNumber);
    return;
    }
    _cultureInfo = new CultureInfo(value);
    }
    }

    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.

    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 .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)
    static cultureinfo GetCultureInfo(string name, bool predefinedOnly)

    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.
  5. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  6. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  7. added
    Resolution-No Activity
    on Nov 15, 2023
  8. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  9. microsoft-github-policy-service commented on Nov 28, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageResolution-No Activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions