Skip to content

Sort-Object -Stable -Unique is not always stable #16907

Description

@m1k0net

Prerequisites

Steps to reproduce

When running data through Sort-Object -Stable -Unique given a specific property the returned results do not appear to be the 1st element seen from a stable sort. Also modifying modifying the input data for an unrelated record effects the records of another. Below is some sample code that can be used to reproduce the issue. The only difference from the data in the 1st set to the 2nd is the last row was changed from 2021 to 2015 which is effecting the file1 key but the file0 key is consistently showing the last item. The rest of the records appear to process correctly.

$Data="Key,Path,Size
file1,C:\folder\2015,10
file1,C:\folder\2018,10
file1,C:\folder\2020,10
file2,C:\folder\2014,10
file2,C:\folder\2019,10
file4,C:\folder\2020,10
file4,C:\folder\2017,10
file4,C:\folder\2020,10
file4,C:\folder\2019,10
file4,C:\folder\2021,10
file0,C:\folder\2015,10
file0,C:\folder\2018,10
file0,C:\folder\2020,10
file2,C:\folder\2022,10
file3,C:\folder\2019,10
file3,C:\folder\2018,10
file3,C:\folder\2019,10
file3,C:\folder\2020,10
file3,C:\folder\2022,10
file3,C:\folder\2021,10"
$List=$Data | ConvertFrom-Csv  
$SortedList=$List | Sort-Object Path -Descending
Write-Host "Sorted List:"
$SortedList | Format-Table
Write-Host "Unique List:"
$SortedList | Sort-Object Key -Unique -Stable | Format-Table


$list[19].Path="C:\folder\2015"
$SortedList=$List | Sort-Object Path -Descending
Write-Host "Sorted List:"
$SortedList | Format-Table
Write-Host "Unique List:"
$SortedList | Sort-Object Key -Unique -Stable | Format-Table```


### Expected behavior

```console
Key   Path           Size
---   ----           ----
file0 C:\folder\2020 10
file1 C:\folder\2020 10
file2 C:\folder\2022 10
file3 C:\folder\2022 10
file4 C:\folder\2021 10

Actual behavior

Case 1:
Key   Path           Size
---   ----           ----
file0 C:\folder\2015 10
file1 C:\folder\2015 10
file2 C:\folder\2022 10
file3 C:\folder\2022 10
file4 C:\folder\2021 10

Case 2:
Key   Path           Size
---   ----           ----
file0 C:\folder\2015 10
file1 C:\folder\2020 10
file2 C:\folder\2022 10
file3 C:\folder\2022 10
file4 C:\folder\2021 10

Error details

No response

Environment data

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


Name                           Value
----                           -----
PSVersion                      7.3.0-preview.1
PSEdition                      Core
GitCommitId                    7.3.0-preview.1
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

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-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions