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
@PowerShell/powershell-committee reviewed this. Although users should not rely on the order, they could be broken by this if they wanted the first group. We agreed that this change should be taken and the impact of someone dependent on previous behavior is low.
Well, I was just about to file a new issue about this, because this impacted me. Here's what I was going to file:
In PowerShell 5.x and below, the Group-Object cmdlet does not sort the groups by the property used to group the objects. In PowerShell 6.1, Group-Object is sorting the groups by the property used to group the objects.
I ran into this as an issue while implementing some changes in Pester. The results of tests are grouped by Contexts (for Gherkin style tests), and the result of the Context (e.g. passed, failed, etc.) is determined from the results of the individual steps that form the members of the group. There are unit tests which check that the results are grouped according to the Context status. The check uses an array of expected context names for a given result status, which is compared with the context names coming from the Pester TestResult object. This object uses the Group-Object cmdlet to perform that sorting. As a consequence, on PowerShell 6.1, these unit tests are broken because the results are not in the same order as the expected array results.
That thought occurred to me. What I wasn't sure about was whether or not the order of the names of the scenarios as reported in the test results object was important. Because that what you said would be the simplest solution and is the first one I thought of.
Craig E. Shea (@fourpastmidnight) See Steves comment above. This is a breaking change, in that it changes the order of the output, but it was deemed to be worth it for the perf improvements.
I saw that. That is a dramatic improvement! There is, however, no mention of this breaking change in the documentation on docs.microsoft.com. This is a fairly significant change. In most cases, people were probably already piping the results to `Sort-Object` or something equivalent. But I’m sure there are others like me who View this cmdlet as another Linq-like PS “operator” and that it would simply stream over the results, preserving the order of the results. I have to remember it’s like Linq, it’s not Linq.
Anyway, I managed to find another way to accomplish what I was trying to do which did not necessitate the use of `Group-Object`, thereby avoiding the issue entirely.
Thank you for replying to me in the first place on this closed issue. I appreciate your time and consideration.
Sent from my Windows 10 phone
From: Staffan Gustafsson
Sent: Friday, April 5, 2019 4:08
To: PowerShell/PowerShell
Cc: Craig E. Shea; Mention
Subject: Re: [PowerShell/PowerShell] Improve performance of Group-Object(#7409)
@fourpastmidnight See Steves comment above. This is a breaking change, in that it changes the order of the output, but it was deemed to be worth it for the perf improvements.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Staffan Gustafsson (@powercode) have we created a docs issue for it? It is a notable change and should definitely be mentioned in the help for the cmdlet.
Activity
powercode commentedon Jul 30, 2018
I submitted my implementation since I figured it is easier to reason about and review the change when it is more concrete.
powercode commentedon Jul 30, 2018
Changed the sorting to a stable sort.
Now it is only the order of the
GroupInfos that are changed, but not the objects inGroupInfo.Group.PetSerAl commentedon Aug 1, 2018
How does it handles?
powercode commentedon Aug 1, 2018
PetSerAl , I like how you think! I just made a special case when the types differ, and fall back to the slower algorithm.
Update:
I maintain the behavior of windows powershell.
SteveL-MSFT commentedon Aug 1, 2018
@PowerShell/powershell-committee reviewed this. Although users should not rely on the order, they could be broken by this if they wanted the first group. We agreed that this change should be taken and the impact of someone dependent on previous behavior is low.
powercode commentedon Aug 2, 2018
Performance graph - before is the seven column to the left, showing the quadratic nature of the number of groups.
The others are from this PR.
powercode commentedon Aug 3, 2018
Doug Finke (@dfinke) Of course,
Export-Excelwas used to generate the graphs! Kudos!fourpastmidnight commentedon Apr 4, 2019
Well, I was just about to file a new issue about this, because this impacted me. Here's what I was going to file:
In PowerShell 5.x and below, the
Group-Objectcmdlet does not sort the groups by the property used to group the objects. In PowerShell 6.1,Group-Objectis sorting the groups by the property used to group the objects.I ran into this as an issue while implementing some changes in Pester. The results of tests are grouped by Contexts (for Gherkin style tests), and the result of the Context (e.g. passed, failed, etc.) is determined from the results of the individual steps that form the members of the group. There are unit tests which check that the results are grouped according to the Context status. The check uses an array of expected context names for a given result status, which is compared with the context names coming from the Pester TestResult object. This object uses the
Group-Objectcmdlet to perform that sorting. As a consequence, on PowerShell 6.1, these unit tests are broken because the results are not in the same order as the expected array results.Steps to reproduce
Expected behavior
Actual behavior
Environment data
vexx32 commentedon Apr 4, 2019
Conceivably, you could refactor your test to take this into account, no?
$Groups.Name | Sort | Should -Be ($Items.Key | Sort)fourpastmidnight commentedon Apr 4, 2019
That thought occurred to me. What I wasn't sure about was whether or not the order of the names of the scenarios as reported in the test results object was important. Because
thatwhat you said would be the simplest solution and is the first one I thought of.powercode commentedon Apr 5, 2019
Craig E. Shea (@fourpastmidnight) See Steves comment above. This is a breaking change, in that it changes the order of the output, but it was deemed to be worth it for the perf improvements.
fourpastmidnight commentedon Apr 5, 2019
vexx32 commentedon Apr 5, 2019
Staffan Gustafsson (@powercode) have we created a docs issue for it? It is a notable change and should definitely be mentioned in the help for the cmdlet.
SteveL-MSFT commentedon Apr 5, 2019
Created MicrosoftDocs/PowerShell-Docs#4121