Skip to content

Incorrect sort order when using tab completion and listing items #19736

Closed

Description

@rboxman

Prerequisites

Steps to reproduce

I am inside a directory that looks as follows. Notice how it's listing out files in the incorrect order! This applies to tab completion as well and the "broken" items will be completed before the correct items. There must be a sort-order bug someplace...

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/29/2023  5:26 AM           6060 check_cmake_consistency_config.py    <-- broken; wrong order
-a---           5/29/2023  5:26 AM          14928 check_cmake_consistency.py
-a---           5/29/2023  5:26 AM           5119 check_deprecated.py
-a---           5/29/2023  5:26 AM           6025 check_descriptions.py
-a---           5/29/2023  5:26 AM           2147 check_header_duplicate.py
-a---           5/29/2023  5:26 AM          15793 check_licenses.py
-a---           5/29/2023  5:26 AM           3792 check_mypy_config.py    <-- broken; wrong order
-a---           5/29/2023  5:26 AM           2908 check_mypy.py
-a---           5/29/2023  5:26 AM          13648 check_spelling_c_config.py    <-- broken; wrong order
-a---           5/29/2023  5:26 AM          23475 check_spelling.py
-a---           5/29/2023  5:26 AM           2524 check_unused_defines.py

Expected behavior

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/29/2023  5:26 AM          14928 check_cmake_consistency.py
-a---           5/29/2023  5:26 AM           6060 check_cmake_consistency_config.py
-a---           5/29/2023  5:26 AM           5119 check_deprecated.py
-a---           5/29/2023  5:26 AM           6025 check_descriptions.py
-a---           5/29/2023  5:26 AM           2147 check_header_duplicate.py
-a---           5/29/2023  5:26 AM          15793 check_licenses.py
-a---           5/29/2023  5:26 AM           2908 check_mypy.py
-a---           5/29/2023  5:26 AM           3792 check_mypy_config.py
-a---           5/29/2023  5:26 AM          23475 check_spelling.py
-a---           5/29/2023  5:26 AM          13648 check_spelling_c_config.py
-a---           5/29/2023  5:26 AM           2524 check_unused_defines.py

Actual behavior

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/29/2023  5:26 AM           6060 check_cmake_consistency_config.py    <-- broken; wrong order
-a---           5/29/2023  5:26 AM          14928 check_cmake_consistency.py
-a---           5/29/2023  5:26 AM           5119 check_deprecated.py
-a---           5/29/2023  5:26 AM           6025 check_descriptions.py
-a---           5/29/2023  5:26 AM           2147 check_header_duplicate.py
-a---           5/29/2023  5:26 AM          15793 check_licenses.py
-a---           5/29/2023  5:26 AM           3792 check_mypy_config.py    <-- broken; wrong order
-a---           5/29/2023  5:26 AM           2908 check_mypy.py
-a---           5/29/2023  5:26 AM          13648 check_spelling_c_config.py    <-- broken; wrong order
-a---           5/29/2023  5:26 AM          23475 check_spelling.py
-a---           5/29/2023  5:26 AM           2524 check_unused_defines.py

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Microsoft Windows 10.0.22621
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

Activity

  1. added
    Needs-Triage
    on Jun 2, 2023
  2. jhoneill commented on Jun 3, 2023

    @jhoneill

    Those are in the correct order.
    The .Net string method is local-culture aware and does not sort by strict character codes. In English Å is sorted before B, even though the character code is miles away. In Norwegian it sorts after Z.
    You can see how it works with

    32..255 | foreach-object {[string]::new($_)} | Sort-Object -culture en-gb | ForEach-Object {"$_  "+ [int][char]$_  }
    

    try other cultures like no-no

    So with your file names.

    > "check_cmake_consistency_config.py"  -lt "check_cmake_consistency.py"
    True
    > "_" -lt "."        # string "_" is before "." 
    True
    > [int][char]"_" -lt [int][char]"."   #char code for "_" is AFTER char code for "."
    False
    

    IIRC PowerShell asks .NET for a list of items in the directory, and .NET returns them pre-sorted, and they are not re-sorted by PowerShell. You could sort by basename in this case (name is basename . extension)

    Note that for some people .NET sorting where A, Á , a and á coming before B and b is looks wrong and A and B should be in one place and a and b in a second and á somewhere else. For most people, most of the time the localized .net way is better, but for some of us, seeing it not in strict ASCII order induces something like sea-sickness

  3. mklement0 commented on Jun 3, 2023

    @mklement0
    Contributor

    .NET's sorting behavior on Windows changed with .NET 5's move to cross-platform, ICU-based internationalization, as described in .NET globalization and ICU.
    PowerShell v7.1+ is based on .NET 5+

    Therefore, the sort order of _ vs. . is now different relative to Windows PowerShell (based on .NET Framework, which uses Windows-specific NLS sorting rules), and explains your experience:

    # WinPS:    -> '.', '_' 
    # PS v7.1+: -> '_', '.' 
    '.', '_' | Sort-Object

    In PS Core, Get-ChildItem performs its own sorting on all platforms, using case-insensitive current-culture sorting (as Sort-Object does by default):

    // On some systems, this is already sorted. For consistency, always sort again.
    IEnumerable<FileSystemInfo> sortedChildList = childList.OrderBy(static c => c.Name, StringComparer.CurrentCultureIgnoreCase);
    .

  4. MartinGC94 commented on Jun 3, 2023

    @MartinGC94
    Contributor

    .NET's sorting behavior on Windows changed with .NET 5's move to cross-platform, ICU-based internationalization, as described in .NET globalization and ICU.

    Neat. From that article it seems you can see an environment variable to revert this change in behavior:
    image

    Of course you should be careful about setting this because it affects more than just sorting but it's good to know that users have some control over this.

  5. rboxman commented on Jun 3, 2023

    @rboxman
    Author

    Michael Klement (@mklement0) Indeed! Wooo! Thanks for that link. I'm just going to modify the pwsh runtime config to use "System.Globalization.UseNls": true, which seems to work. I can probably move on from using pwsh 7.0.x now since that was the last series that was sane.

    Finally, tab-completion order that makes sense!

    What a bone-headed decision this is. Doesn't align with what Explorer shows. Doesn't align with what Linux shows in their UIs or cmd lines. I hope they never drop this Nls option.

  6. mklement0 commented on Jun 3, 2023

    @mklement0
    Contributor

    Thanks, MartinGC94, good to know.
    It's implied by your screenshot, but let me spell it out: the environment variable must be defined before PowerShell is launched in order to take effect.

    Glad to hear the link helped, rboxman.

    Do note that by configuring PowerShell with the NLS opt-in you'll fundamentally change how string sorting works in your session, along with other behaviors, as MartinGC94 notes (the areas are affected are listed near the top of .NET globalization and ICU.

    What a bone-headed decision this is. Doesn't align with what Explorer shows.

    I suspect it wasn't so much an explicit decision on the PowerShell side but an accidental fallout from the change in .NET (though it sounds like PS Core did exhibit the _ before . behavior from the beginning on Unix, where globalization was always ICU-based).

    • File Explorer, as an unmanaged application, still uses NLS, and additionally has custom-sorting logic for numbers embedded in file names even without fixed-width padding.

      • Similarly, cmd.exe's dir command uses NLS.
    • On Unix platforms:

      • [Update: Originally I thought that ls on Linux behaves like PS Core with a locale such as en_US.utf8, but that turns out not to be true.]
      • On both macOS and Ubuntu (Linux) ls indeed sorts . before _, as in Windows PowerShell / File Explorer.
      • This is curious, as I would expect all native utilities to use the same ICU libraries as .NET, but sort too places . before _, unlike .NET
  7. rboxman commented on Jun 3, 2023

    @rboxman
    Author

    The WSL2 variant of Ubuntu seems to be using C.UTF-8 by default which is what my comparisons were against.

  8. mklement0 commented on Jun 3, 2023

    @mklement0
  9. added
    Resolution-Answered
    Resolution-External
    on Jun 4, 2023
  10. mklement0 commented on Jun 4, 2023

    @mklement0
    Contributor

    My bad, rboxman: ls on Linux does place . before _, after all, even with a proper locale such as en_US.utf8 - my tests must have been flawed.
    The following command - which must be run from Bash - demonstrates this:

    # -> 
    # a.b
    # a_b
    LANG=en_US.utf8 sh -c 'mkdir -p /tmp/$$ && cd /tmp/$$ && touch a.b a_b && ls -1 && rm -r /tmp/$$'

    As noted, I'm mystified by different sorting behavior also occurring with sort, as I would expect the same ICU libraries to be used both by the native utilities and .NET.

  11. removed
    Needs-Triage
    on Jun 5, 2023
  12. jhoneill commented on Jun 5, 2023

    @jhoneill

    Since Ryan Yates (@kilasuit) has marked this as resolved I've removed the needs-triage flag.
    rboxman please say if you're unhappy with either.

  13. rboxman commented on Jun 6, 2023

    @rboxman
    Author

    Yeah, as long as the NLS option remains available to pwsh it's fine and you can probably close this issue.

  14. jhoneill commented on Jun 6, 2023

    @jhoneill

    rboxman Thanks. The bot will mark this closed after a 24 hours of inactivity.

  15. ghost closed this as completedon Jun 7, 2023
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

    Resolution-AnsweredResolution-External

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions