Skip to content

Sort order difference between PowerShell 5 and PowerShell 7 #20730

@jason-nyc

Description

@jason-nyc

Prerequisites

Steps to reproduce

I experienced a slightly different sort order when sorting a list of files (strings) by name. Upon further review, I found that, for instance, the underline (95) and the period (46) are sorted differently.

In PowerShell 5, the underline sorts before the period.
In PowerShell 7, the underline sorts after the period.

This is on the same machine, the same get-culture.

I solved this by converting to an ArrayList and performing an Ordinal sort.
$list = New-Object System.Collections.ArrayList;$list.AddRange($mystrings ) $list.Sort([System.StringComparer]::Ordinal)

If I need to stick with Sort-Object, I convert the string to Hex strings of Unicode bytes and sort by that.
$mystrings | Sort-Object {[system.Text.Encoding]::UTF8.GetBytes($_)|ForEach-Object ToString X2}
Note that Sort-Object has a -culture option, but I couldn't find an InvariantCulture to use.

Expected behavior

# PowerShell 5
#
PS> "_" -gt "."
True
PS> $mystrings = "A","B","C","a","b","c","_","."
$mystrings | Sort-object
.
_
A
a
B
b
C
c
PS> $mystrings | Sort-object | %{[int][char]$_}
46
95
65
97
66
98
67
99

Actual behavior

# PowerShell 7
#
PS> "_" -gt "."
False
PS> $mystrings = "A","B","C","a","b","c","_","."
PS> $mystrings | Sort-object
_
.
A
a
B
b
C
c
PS> $mystrings | Sort-object | %{[int][char]$_}
95
46
65
97
66
98
67
99

Error details

No response

Environment data

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.9
PSEdition                      Core
GitCommitId                    7.3.9
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

$PSVersionTable

Name                           Value                                                                                                                                
----                           -----                                                                                                                                
PSVersion                      5.1.22621.2506                                                                                                                       
PSEdition                      Desktop                                                                                                                              
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                              
BuildVersion                   10.0.22621.2506                                                                                                                      
CLRVersion                     4.0.30319.42000                                                                                                                      
WSManStackVersion              3.0                                                                                                                                  
PSRemotingProtocolVersion      2.3                                                                                                                                  
SerializationVersion           1.1.0.1

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions