2

You probably know that Finder uses natural sort order rather than ASCII order, when sorting files by name. See:

Recently, I was trying to reproduce the exact sorting behavior of Finder in some code. Punctuation and symbols come before letters and numbers, but I do not understand the ordering within the class of punctuation marks.

I found a blog post (original source here) where the ordering for some special characters was worked out. I did some experimenting to fill in the gaps in the table. The complete ordering (with ASCII values) appears to be as follows:

_  underscore           0x5f
-  dash (hyphen)        0x2d
,  comma                0x2c
;  semicolon            0x3b
!  exclamation point    0x21
?  question mark        0x3f
.  period               0x2e
'  single quote         0x27
"  double quote         0x22
(  open parenthesis     0x28
)  close parenthesis    0x29
[  open bracket         0x5b
]  close bracket        0x5d
{  open brace           0x7b
}  close brace          0x7d
@  at symbol            0x40
*  asterisk             0x2a
/  forward slash        0x2f
\  backward slash       0x5c
&  ampersand            0x26
#  number sign          0x23
%  percent sign         0x25
`  backtick             0x60
^  caret                0x5e
+  plus sign            0x2b
<  less than            0x3c
=  equal sign           0x3d
>  greater than         0x3e
|  vertical bar         0x7c
~  tilde                0x7e
$  dollar sign          0x24

Can someone explain to me the logic behind this ordering?

  • Which language/regional setting is active on your Mac? – nohillside May 18 at 18:04
  • Ah, just tried with German/Swiss German and got the same order, so it's probably not depending on the locale (at least not in Finder). – nohillside May 18 at 18:09
  • LANG=en_US.UTF-8 – D. Ray May 18 at 18:10
  • “Natural Sort Order” is based on a string sort, so it’s treating all values as strings. – Allan May 18 at 18:32
  • 1
    @klanomath: the thought process of the programmer implementing the algorithm. The symbols in the table above are not in ASCII order -- indeed, they are quite scrambled with respect to ASCII -- so why was this particular order chosen? Why deviate from ASCII order seemingly for no reason? (By contrast, digit strings are grouped together into numbers for a clear and sensible reason -- to make sort results more readable to non-programmers.) Is it completely arbitrary, or is there some reasoning behind it that I'm not seeing? – D. Ray May 18 at 23:36

You must log in to answer this question.

Browse other questions tagged .