Skip to content

Sort-Object returns different results on default OS configs for Windows vs. Ubuntu 18.04 #8824

Closed

Description

@adamedx

When I use sort-object on PowerShell Core on Windows vs. Ubuntu 18.04, the results are sorted differently using the operating systems out of the box. IMO, the Windows behavior is correct, if for no other reason that it is consistent with PowerShell Desktop. In general, if a language such as Javascript were to suddenly release a change that made sorting primitives native to the language behave differently on Windows vs. Linux / Mac, the web as we know it would break completely.

Steps to reproduce

Run the following command on PowerShell Core 6.2.0 preview3 on Windows, and on Ubuntu 18.04:

$PSVersionTable.Platform;('a', 'ab', 'aC' | Sort-Object ) -join ','

Expected behavior

The only difference should be the Platform string, Windows vs. Linux, that is output -- the sorted object output should be the same on both platforms, like this:

# The first line below is executed on Windows, the second on Ubuntu 18.04
PS> $PSVersionTable.Platform;('a', 'ab', 'aC' | Sort-Object ) -join ','
Win32NT
a,ab,aC
PS> $PSVersionTable.Platform;('a', 'ab', 'aC' | Sort-Object ) -join ','
Unix
a,ab,aC

Actual behavior

Not only are the platforms different, but Windows returns the objects in a different (correct IMO) order than on Linux!

# The first line below is executed on Windows, the second on Ubuntu 18.04
PS> $PSVersionTable.Platform;('a', 'ab', 'aC' | Sort-Object ) -join ','
Win32NT
a,ab,aC
PS> $PSVersionTable.Platform;('a', 'ab', 'aC' | Sort-Object ) -join ','
Unix
a,aC,ab

Impact

This is pretty serious -- by default, scripts running on Windows and Linux will return different results for something as canonical as sorting text strings. This breaks modules in some obvious ways for me at the moment (in my case, parameter completion is broken as it relies on sorting), but it also means there are subtle changes to fundamental assumptions of scripts that may result in seemingly non-deterministic behavior that is difficult to reproduce and apparently random. Diagnosing the root cause may also be difficult as few people would expect a built-in function like sort-object to exhibit this behavior.

Certainly the precedent set by other dynamic languages such as Javascript, Python, or Ruby is that the behavior is consistent when using built-in language primitives to sort.

Notes

Per this comment on issue 8604, I tried the suggestion that configuring the LC_ALL variable on Linux would affect this, and it turns out that this is indeed the case, as demonstrated by running the following commands on bash on Linux:

# Works fine
LC_ALL=en_US.UTF-8 pwsh-preview -noprofile -c "echo \$PSVer
sionTable.PSVersion;('a', 'ab', 'aC' | Sort-Object ) -join ','"

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
6      2      0      preview.3
a,ab,aC

# Broken!
LC_ALL=C pwsh-preview -noprofile -c "echo \$PSVersionTable.PSVersion;('a', 'ab', 'aC' | Sort-Object ) -join ','"

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
6      2      0      preview.3
a,aC,ab

Environment data

PSVersionTable Output:

PS /mnt/c/Users/adamed/src/sdk/autographps-sdk> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.2.0-preview.3
PSEdition                      Core
GitCommitId                    6.2.0-preview.3
OS                             Linux 4.4.0-17763-Microsoft #253-Microsoft Mon Dec 31 17:49:00 PST 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

In general I would agree with the comment on issue 8604 that the unix sort command doesn't exhibit this behavior even with LC=C. I would think that if the issue comes from .net core, it should be fixed there (what does mono do?), but if not, it seems like a fix is needed on PowerShell Core -- this is a huge portability problem and also goes against what JavaScript, Python, and Ruby have set as a standard.

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Feb 4, 2019
  2. iSazonov commented on Sep 29, 2019

    @iSazonov
    Collaborator

    From https://github.com/dotnet/corefx/issues/28611 comments it is "by design".

  3. added a commit that references this issue on Oct 24, 2021
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

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions