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
{{ message }}
Repository navigation
Sort is incorrect for strings containing the '-' character #3425
Character sets include ignorable characters. The Compare(String, String, Boolean) method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a culture-sensitive, case-insensitive comparison of "animal" with "Ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.
Agree that this is not a bug, but a design choice.
However it is confusing behaviour to many who don't expect this, and is not the desirable behaviour in a number of use cases.
Amending this default behaviour would be a breaking change.
However, adding a parameter to allow users to define the sort behaviour, or adding some field to the property parameter's hash table would resolve this limitation without negatively affecting existing behaviour, and would help people realise that the current behaviour is the designed behaviour.
Proposal
The Property parameter accepts a collection of hash tables, where the hash table accepts keys Expression, Ascending and Descending.
Adding another key, SortOrderComparer, which takes a property of type IComparer would allow custom sort behaviour to be specified for each property. Thus to get the behaviour most people would expect, they could do something like this:
I agree that we could resolve the Issue by means of adding a new parameter to set a comparer options. It seems we should be more general then StringComparer.
Reacted by John Bevan, Troy Campbell, Michael Klement and Rain Sallow (/u/ta11ow)
I suggest also supporting Comparison<T> delegates as the SortOrderComparer value (polymorphically), so you can pass script blocks directly (e.g., { param([string]$x, [string]$y) <# return -1, 0, or 1 #> } and perhaps shortening SortOrderComparer to Comparer.
With a Comparer key present, Expression should be optional and default to the whole input object.
For anyone hitting this looking for a work around, this appears to work for at least our use case:
# Sorting file names that include '-' and '.' is broken in powershell - https://github.com/PowerShell/PowerShell/issues/3425
# So map each to characters invalid for file system use '?' and '|', sort, and then map back
function Sort-FileSafe ($names) {
return $names | % {$_.replace("-","?")} | % {$_.replace(".","|")} | Sort-Object | % {$_.replace("?","-")} | % {$_.replace("|",".")}
}
We had two different Windows machine consistently generating different ordered lists of file names with those characters.
Activity
iSazonov commentedon Mar 25, 2017
It looks as .Net issue (tested on Windows PowerShell and PowerShell Core):
PetSerAl commentedon Mar 25, 2017
Then it is Windows issue, because for displaying purposes Windows sort filenames in the same order.
iSazonov commentedon Mar 25, 2017
iSazonov commentedon May 1, 2017
From MSDN String.Compare Method:
Unicode Default_Ignorable_Code_Point
Best Practices for Using Strings in the .NET Framework
Based on this we should re-label the problem as internal.
iSazonov commentedon Jun 4, 2017
Joey Aiello (@joeyaiello) Stephen Toub (@stephentoub) The Issue is internal. It seems we need PowerShell-Committee review.
JohnLBevan commentedon Jun 26, 2017
Agree that this is not a bug, but a design choice.
However it is confusing behaviour to many who don't expect this, and is not the desirable behaviour in a number of use cases.
Amending this default behaviour would be a breaking change.
However, adding a parameter to allow users to define the sort behaviour, or adding some field to the
propertyparameter's hash table would resolve this limitation without negatively affecting existing behaviour, and would help people realise that the current behaviour is the designed behaviour.Proposal
The
Propertyparameter accepts a collection of hash tables, where the hash table accepts keysExpression,AscendingandDescending.Adding another key,
SortOrderComparer, which takes a property of typeIComparerwould allow custom sort behaviour to be specified for each property. Thus to get the behaviour most people would expect, they could do something like this:iSazonov commentedon Jun 26, 2017
I agree that we could resolve the Issue by means of adding a new parameter to set a comparer options. It seems we should be more general then StringComparer.
mklement0 commentedon Jul 15, 2018
Great idea, John Bevan (@JohnLBevan).
I suggest also supporting
Comparison<T>delegates as theSortOrderComparervalue (polymorphically), so you can pass script blocks directly (e.g.,{ param([string]$x, [string]$y) <# return -1, 0, or 1 #> }and perhaps shorteningSortOrderComparertoComparer.With a
Comparerkey present,Expressionshould be optional and default to the whole input object.iSazonov commentedon Jul 12, 2019
For reference PowerShell/PowerShell-RFC#167
chamons commentedon Apr 22, 2021
For anyone hitting this looking for a work around, this appears to work for at least our use case:
We had two different Windows machine consistently generating different ordered lists of file names with those characters.
chamons commentedon Aug 25, 2021
Was this fixed, or considered invalid? I don't see any reasoning here, or on the RFC.
iSazonov commentedon Aug 26, 2021
We see near-zero activity and complete indifference from MSFT team.
So I have no desire to keep track of dead issue.
mklement0 commentedon Mar 9, 2024
The problem implicitly went away in 7.1, due to .NET 5+'s move to the ICU internationalization library - see .NET globalization and ICU