Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Sort order of alphanumeric file names in Finder

File Name Sorting
I often using dates for file names - for letters and for photos in particular, with a format something like yymmdd, e.g., today's date, December 27, would be 071227. Occasionally I may use 20071227. If I have the year and month but do not have a specific date, the file name might look like 0712xx. The sort sequence of files in the various Finder views does not seem to be what I would expect, based on standard computer alphanumeric sorting rules. Here is what I am finding - three files beginning with file names 0708xx, 070212 and 070521 will appear in the Finder list as follows:

0708xx
070212
070521

whereas I would expect the proper sort order to be:

070212
070521
0708xx

If I change "xx" to "00" then the sort order is strictly numerical, i.e.,

070212
070521
070800

No one can explain why the files sort per the first list above, if I use the "xx" convention for the date. Is there a Leopard bug, or is the sort order based on an algorithm I am unfamiliar with.

iMac, Mac OS X (10.5.1)

Posted on Dec 31, 2007 9:18 AM

Reply
Question marked as Best reply

Posted on Dec 31, 2007 9:36 AM

The Finder does have its own rules on sorting, but I can't reproduce what you describe, so I'm not sure if that's the issue or not.

The Finder implements a numeric sort for file names that begin with digits. This is counter to a purely alphabetic sort that you might expect?

What's the difference? Create a series of files named from 1 through 100.
In an alphabetic sort you'll see:

1
10
11
12
13
14
15
16
17
18
19
2
20
21
etc.


In other words, the file '2' appears after all the files that begin with '1'.

In comparison, the Finder will list this as:

1
2
3
4
5
6
7
8
9
10
11
12
etc.


In other words, the Finder considers numerical value over alphabetic characters.

Now, that said, your '0708' should sort after '0702' whether you're sorting numerically or alphabetically. As I said, I can't reproduce what you're seeing, so there must be some other force at work.
4 replies
Question marked as Best reply

Dec 31, 2007 9:36 AM in response to new-ken

The Finder does have its own rules on sorting, but I can't reproduce what you describe, so I'm not sure if that's the issue or not.

The Finder implements a numeric sort for file names that begin with digits. This is counter to a purely alphabetic sort that you might expect?

What's the difference? Create a series of files named from 1 through 100.
In an alphabetic sort you'll see:

1
10
11
12
13
14
15
16
17
18
19
2
20
21
etc.


In other words, the file '2' appears after all the files that begin with '1'.

In comparison, the Finder will list this as:

1
2
3
4
5
6
7
8
9
10
11
12
etc.


In other words, the Finder considers numerical value over alphabetic characters.

Now, that said, your '0708' should sort after '0702' whether you're sorting numerically or alphabetically. As I said, I can't reproduce what you're seeing, so there must be some other force at work.

Dec 31, 2007 9:54 AM in response to Camelot

..." As I said, I can't reproduce what you're seeing, so there must be some other force at work."...

Could it be that there is in fact something atypical about your system? I had a thread in the unix section regarding folder permissions where you were unable to reproduce a behaviour that I was seeing on many machines, and which another user was subsequently able to confirm.

Here, my system behaves as the OPs - the number 708 is smaller than 70212 is smaller than 70521 so 0708xx sorts before 070212, which sorts before 070521. The numerical part of 0708xx is interpreted separately.

I consider it counter-intuitive and makes keyboard based file selection cumbersome compared to straight alphanumeric sorting, but this has been how OS X "Finder" has sorted, probably since 10.0.

Dec 31, 2007 10:20 AM in response to new-ken

No one can explain why the files sort per the first list above, if I use the "xx" convention for the date. Is there a Leopard bug, or is the sort order based on an algorithm I am unfamiliar with.


This is standard OS X file sorting behavior. Numerical strings are sorted by their arithmetic value. See this note:

http://discussions.apple.com/thread.jspa?threadID=121936&tstart=105

Dec 31, 2007 12:05 PM in response to Tom Gewecke

Tom's comprehensive article provides the answer but is perhaps a bit much for those looking for a simple, easy to remember rule. The brief one at File System Overview: Sorting Rules may be useful in this respect:

"Digit sub-strings are sorted by numeric value rather than as characters."

IOW, much like a human would naturally do, the Finder considers numeric expressions as potentially multi-character entities.

Except ... just like humans, it seems not to know what to do with leading zeros. Try this:

Create a new folder & add to it 4 sub-folders named as follows (all "0" characters are zeros):

2
2aa
02
02aa

Note that in list view, Leopard sorts them by name as follows:

02
2
02aa
2aa

But, try selecting them in order by hitting the tab key repeatedly, which should move you down the list one item after the other ... but doesn't.

Sort order of alphanumeric file names in Finder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.