Skip to content

Sort is incorrect for strings containing the '-' character #3425

Closed

Description

@iSazonov

From UserVoice https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/18580849-bug-sort-is-incorrect-for-strings-containing-the

Steps to reproduce

"somefile1","somefile2","s-abc","s-little","s-foo","s-poo","s-wtf" | sort

Expected behavior

s-abc
s-foo
s-little
s-poo
s-wtf
somefile1
somefile2

Actual behavior

s-abc
s-foo
s-little
somefile1
somefile2
s-poo
s-wtf

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSVersion                      6.0.0-alpha
PSEdition                      Core
BuildVersion                   3.0.0.0
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.3
CLRVersion
WSManStackVersion              3.0
GitCommitId                    v6.0.0-alpha.17

Activity

  1. added
    Issue-Bug
    on Mar 25, 2017
  2. iSazonov commented on Mar 25, 2017

    @iSazonov
    CollaboratorAuthor

    It looks as .Net issue (tested on Windows PowerShell and PowerShell Core):

    PS C:\WINDOWS\system32> using namespace System.Collections.Generic
    PS C:\WINDOWS\system32> $a = New-Object List[string]
    PS C:\WINDOWS\system32> "somefile1","somefile2","s-abc","s-little","s-foo","s-poo","s-wtf" | % {$a.Add($_)}
    PS C:\WINDOWS\system32> $a
    somefile1
    somefile2
    s-abc
    s-little
    s-foo
    s-poo
    s-wtf
    PS C:\WINDOWS\system32> $a.Sort()
    PS C:\WINDOWS\system32> $a
    s-abc
    s-foo
    s-little
    somefile1
    somefile2
    s-poo
    s-wtf
  3. PetSerAl commented on Mar 25, 2017

    @PetSerAl
    Contributor

    Then it is Windows issue, because for displaying purposes Windows sort filenames in the same order.

  4. iSazonov commented on Mar 25, 2017

    @iSazonov
    CollaboratorAuthor
    PS C:\WINDOWS\system32> [string]::Compare("som","s-l")
    1
    PS C:\WINDOWS\system32> [string]::Compare("som","s-m")
    1
    PS C:\WINDOWS\system32> [string]::Compare("som","s-p")
    -1
  5. added
    Resolution-External
    on Apr 20, 2017
  6. iSazonov commented on May 1, 2017

    @iSazonov
    CollaboratorAuthor

    From MSDN String.Compare Method:

    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.

    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.

  7. iSazonov commented on Jun 4, 2017

    @iSazonov
    CollaboratorAuthor

    Joey Aiello (@joeyaiello) Stephen Toub (@stephentoub) The Issue is internal. It seems we need PowerShell-Committee review.

  8. removed
    Resolution-External
    on Jun 5, 2017
  9. JohnLBevan commented on Jun 26, 2017

    @JohnLBevan
    Contributor

    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:

    [string[]]$list = @("somefile1","somefile2","s-abc","s-little","s-foo","s-poo","s-wtf") 
    $list | sort -Property @{Expression={$_}; SortOrderComparer=[System.StringComparer]::Ordinal}
    
  10. added
    Issue-Enhancement
    and removed
    Issue-Bug
    on Jun 26, 2017
  11. iSazonov commented on Jun 26, 2017

    @iSazonov
    CollaboratorAuthor

    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.

  12. mklement0 commented on Jul 15, 2018

    @mklement0
    Contributor

    Great idea, John Bevan (@JohnLBevan).

    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.

  13. iSazonov commented on Jul 12, 2019

    @iSazonov
    CollaboratorAuthor
  14. chamons commented on Apr 22, 2021

    @chamons

    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.

  15. chamons commented on Aug 25, 2021

    @chamons

    Was this fixed, or considered invalid? I don't see any reasoning here, or on the RFC.

  16. iSazonov commented on Aug 26, 2021

    @iSazonov
    CollaboratorAuthor

    We see near-zero activity and complete indifference from MSFT team.
    So I have no desire to keep track of dead issue.

  17. mklement0 commented on Mar 9, 2024

    @mklement0
    Contributor

    The problem implicitly went away in 7.1, due to .NET 5+'s move to the ICU internationalization library - see .NET globalization and ICU

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-EnhancementWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions