Windows Explorer File Sort

Windows Explorer allows you to sort the files and directories in a folder by various properties by clicking the appropriate column header. Folders always come before files regardless of name, and there is unfortunately no way to change this even though sometimes it is desirable to sort with mixed folders/files.

The algorithm for sorting by name is mostly straightforward but with some bumps. Knowing the sort order allows you to choose characters to use as the first character in a file or folder name to control sorting, for example when creating a filing system.

For the most part, the algorithm sorts files/folders by name according to their ASCII values, expect that case is ignored because on Windows, filenames are case-insensitive, that is “MYFILE”, “MyFile”, and “myfile” are all the same file—iirc NTFS can do case-sensitive but it’s actually a hack (mapping).

You should also be aware that numbers are treated as numbers and not strings. For example “123blah” will come after “2blah” since 123 is bigger than 2, even though “1”-“2”-“3” comes before “2”. This behavior can be changed with the NoStrCmpLogical option of the Explorer policies (set HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoStrCmpLogical to dword:00000001).

Another bump in the algorithm is that the following characters appear out of order: +, <, and > They come after all other characters, in this order. Also, and are ignored altogether when they are the first character.

Here is a list of characters in the order that they will be sorted when used as the first character:

1 – Not (normally) allowed as a filename character
2 – Not in ASCII order
3 – Not used in sort when first character
  (space)
!
1
#
$
%
&
(
)
* 1
,
.
/ 1
: 1
;
? 1
@
[
\ 1
]
^
_
`
{
| 1
}
~
+ 2
< 1,2
= 1
> 1,2
3
3
 

This knowledge will be used in a later article to create a filing system that will allow organization of files and folders since Windows doesn’t really have symbolic links and WinFS has been cancelled until at least Windows Vienna.