Repository navigation
Closed
Labels
Resolution-AnsweredThe question is answered.Resolution-ExternalThe issue is caused by external component(s).
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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.pyActual 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.pyError 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.0Visuals
No response
Metadata
Metadata
Assignees
Labels
Resolution-AnsweredThe question is answered.Resolution-ExternalThe issue is caused by external component(s).
Activity
jhoneill commentedon Jun 3, 2023
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
try other cultures like no-no
So with your file names.
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
basenamein this case (nameisbasename . extension)Note that for some people .NET sorting where
A,Á,aandácoming beforeBandbis looks wrong andAandBshould be in one place andaandbin 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-sicknessmklement0 commentedon Jun 3, 2023
.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:In PS Core,
Get-ChildItemperforms its own sorting on all platforms, using case-insensitive current-culture sorting (asSort-Objectdoes by default):PowerShell/src/System.Management.Automation/namespaces/FileSystemProvider.cs
Lines 1632 to 1633 in 1f02772
MartinGC94 commentedon Jun 3, 2023
Neat. From that article it seems you can see an environment variable to revert this change in behavior:

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.
rboxman commentedon Jun 3, 2023
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.
mklement0 commentedon Jun 3, 2023
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.
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.
cmd.exe'sdircommand uses NLS.On Unix platforms:
lson Linux behaves like PS Core with a locale such asen_US.utf8, but that turns out not to be true.]lsindeed sorts.before_, as in Windows PowerShell / File Explorer.sorttoo places.before_, unlike .NETrboxman commentedon Jun 3, 2023
The WSL2 variant of Ubuntu seems to be using
C.UTF-8by default which is what my comparisons were against.mklement0 commentedon Jun 3, 2023
mklement0 commentedon Jun 4, 2023
My bad, rboxman:
lson Linux does place.before_, after all, even with a proper locale such asen_US.utf8- my tests must have been flawed.The following command - which must be run from Bash - demonstrates this:
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.jhoneill commentedon Jun 5, 2023
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.
rboxman commentedon Jun 6, 2023
Yeah, as long as the NLS option remains available to pwsh it's fine and you can probably close this issue.
jhoneill commentedon Jun 6, 2023
rboxman Thanks. The bot will mark this closed after a 24 hours of inactivity.