Sort-Order: displayed from "File Explorer" vs the results from Cmd.exe and PowerShell; see attached.


Query01

Member
Member
Local time
10:34 AM
Posts
80
Visit site
OS
Windows 11\Home
Please see attached image ...
Code:
OsName                : Microsoft Windows 11 Home
OSDisplayVersion      : 23H2
OsVersion             : 10.0.22631
Code:
PS> Get-ItemPropertyValue  c:\Windows\system32\cmd.exe -name versioninfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
10.0.22621.1     10.0.22621.1 ... C:\Windows\system32\cmd.exe
Code:
PS> Get-ItemPropertyValue c:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -name versioninfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
10.0.22621.1     10.0.22621.1 ... C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
Regardless of "Sort-Order-By Name" being ascending or descending, while using either:
Code:
 cmd.exe> Dir /B
or,
Code:
 PS> Get-ChildItem -Name

... by comparison the following results are respectively the same for both the above command's:

20240906_Testing.m3u
20241128_RB_Easy.m3u
20241218_ToeTap_Fun.m3u
2024_Easy.m3u
Billy_Jack.m3u
Bitter_Earth.m3u
Bitter_Earth_Jazz.m3u
Bitter_Earth_Slow-N-Easy.m3u
Hayes_New_Birth.m3u
Marvin.m3u
Swing_Jazz.m3u

... none of which is an exact match to what the attached "File Explorer" image displays, --no matter if Sorted-By-Name in ascending or descending order. Notice the order in which filename "2024_Easy.m3u" resides. With the default "Sort-Order-By Name" for "File Explorer", I would think "2024_Easy.m3u" would be listed before "20240906_Testing.m3u". It's not working out that way with either:
Code:
 cmd.exe> Dir /B
or,
Code:
 PS> Get-ChildItem -Name
Please advise and correct where needed.
 

Attachments

  • 2025-01-01_003200_Sort-By-Name.webp
    2025-01-01_003200_Sort-By-Name.webp
    13.6 KB · Views: 1

My Computer

System One

  • OS
    Windows 11\Home
Filename sorting can be handled by one of two rules:
- allowing the NTFS FindFirstFile call to provide an almost sorted order (which doesn't abide by the same collation rules)
- or by specifically using the Windows collation rules, based on your current locale

Default file order of "dir" command in Windows console

This subtle change can be demonstrated when you use:
Code:
dir /b
dir /b /o
Code:
Get-ChildItem-Name
Get-ChildItem -Name | Sort-Object

Offhand, it doesn't appear as if File Explorer allows you to chose a different sort ordering.
 

Attachments

  • Windows 10 x64-2024-12-31-22-30-19.webp
    Windows 10 x64-2024-12-31-22-30-19.webp
    83.3 KB · Views: 1
  • Windows 10 x64-2024-12-31-22-31-27.webp
    Windows 10 x64-2024-12-31-22-31-27.webp
    81.2 KB · Views: 1

My Computer

System One

  • OS
    Windows 7
Offhand, it doesn't appear as if File Explorer allows you to chose a different sort ordering.
@garlin -- I don't think the issue is with the way\method "File Explorer" is sorting, it seems to be both cmd.exe and Powershell.exe who are not in-sync, by 'not' sorting the item's in the same order as "File Explorer". Within the example's I submitted, the "File Explorer" sorting, has been the norm since Windows-07; in my experience.

I post here because cmd.exe and Powershell.exe are included with Win-11. The outstanding query: why is there a different sorting method or results with "File Explorer" compared to both cmd.exe and Powershell.exe; both of which have different results?

Respectfully
 

My Computer

System One

  • OS
    Windows 11\Home
The answer is provided by Raymond Chen (who is a principal MS dev), NTFS directories are stored in B-tree's.

A B-tree is a binary search data object, but NTFS has its own sorting algorithm.
The NTFS file system internally maintains directory entries in a B-tree structure, which means that the most convenient way of enumerating the directory contents is in B-tree order, which if you cover one eye and promise not to focus too closely looks approximately alphabetical for US-English. (It’s not very alphabetical for most other languages, and it falls apart once you add characters with diacritics or anything outside of the Latin alphabet, and that includes spaces and digits!)
...
Of course, none of this behavior is contractual. NTFS would be completely within its rights to, for example, return entries in reverse alphabetical order on odd-numbered days. Therefore, you shouldn’t write a program that relies on any particular order of enumeration. (Or even that the order of enumeration is consistent between two runs!)

Explorer returns a result which is ordered by your locale (it is re-sorted again).
dir and Get-ChildItem return the raw NTFS result.

Here is W7:

1735748700973.webp
 
Last edited:

My Computer

System One

  • OS
    Windows 7
The answer is provided by Raymond Chen (who is a principal MS dev), NTFS directories are stored in B-tree's.

A B-tree is a binary search data object, but NTFS has its own sorting algorithm.

Explorer returns a result which is ordered by your locale (it is re-sorted again).
dir and Get-ChildItem return the raw NTFS result.

@garlin -- Thank You,(y) this was most helpful.

I would have never known what to submit, as keyword(s) in a search field\form, that would have retrieved such accurate results; I've never heard of "B-tree Structure". Yet another reason to by-pass the GUI as much as possible without acquiring both eye and headaches during the learning curve. Have a healthy 2025.

Respectfully:win:
 

My Computer

System One

  • OS
    Windows 11\Home

Latest Tutorials

Back
Top Bottom