Natural Order
Numerical Sorting


June 1996: Natural Order makes its debut at MacHack.
February 1997: Natural Order featured in TidBITS.
March 1997: Fetch has been updated to use Natural Order's numerical sorting algorithm.
December 1998: BigSkyMac Ezine features an article on Natural Order.
December 1998: Natural Order is 16th December's tip of the day at www.tipworld.com
June 1999: Natural Order featured as MacWorld's Tip of the Month.
Please let me know of any other articles about Natural Order
or software using Natural Order's numerical sorting algorithm
if you'd like me to add a link here.

Introduction

Natural Order is a System Extension that improves the sorting order that your Macintosh uses when it displays lists of items that have numbers in the name.

If you have a group of numbered files on your disk and you select "View By Name" in the Finder, you might expect the files to be displayed in sensible numerical order, like this:

Picture1.jpg
Picture2.jpg
Picture3.jpg
Picture4.jpg
Picture5.jpg
Picture6.jpg
Picture7.jpg
Picture8.jpg
Picture9.jpg
Picture10.jpg
Picture11.jpg
Picture12.jpg

If you've ever had a group of numbered files on your disk, you'll know that the Finder doesn't do this. When you select "View By Name" what you get looks like this:

Picture1.jpg
Picture10.jpg
Picture11.jpg
Picture12.jpg
Picture2.jpg
Picture3.jpg
Picture4.jpg
Picture5.jpg
Picture6.jpg
Picture7.jpg
Picture8.jpg
Picture9.jpg

This looks stupid, and it's because the System is just sorting the names lexicographically. By the same logic that dictates that "ab" comes alphabetically before "c" (which is correct) the System decides that "12" comes before "3" (which is stupid). D'oh! Natural Order works by overriding the System's comparison of the numerical portions of strings, and sorts them by numerical value instead of "alphabetical" value.

How to use Natural Order

Drop Natural Order into the System Folder and Restart your Mac.

Natural Order works with all Macintosh System Software versions.

How Natural Order works

Natural Order works by overriding the System's IUMagString and IUMagPString functions.

Natural Order only overrides the sorting of the numeric parts of strings. It still uses your System's built-in sorting function for the textual parts of strings, so if you have your system set to some non-US sorting order, that order will still be honoured by Natural Order.

Natural Order uses the following rule to sort strings:

  1. The space character sorts before numbers; numbers sort before non-numbers.
  2. Numbers sort in numerical order; leading zeroes and spaces on numbers are ignored, except as a tie-breaker for numbers that have the same numerical value.
  3. Non-numbers sort in the Mac's System sorting order.

Restrictions

Because Natural Order works by overriding the System's IUMagString and IUMagPString functions, programs that use their own sorting order and do not call the System's comparison routines will not get any benefit from Natural Order.

Where to get Natural Order

You can get Natural Order from the usual Macintosh software archives, such as the MIT Info-Mac HyperArchive or directly from this server.

Using Natural Order in your Software

On a Macintosh, you should tell your users to install Natural Order, and you should change your software call the system's standard International Utility string comparison functions. This way the user gets the benefit of string sorting that conforms to their local language conventions, and you get the benefit of any future improvements in Natural Order without having to update your software.

If for some reason this solution is not suitable for you, you may include the Natural Order sorting algorithm directly in your software at no charge, providing that you credit my contribution in a piece of text such as a copyright or acknowledgements section that is visible to end users of the product.


Page maintained by Stuart Cheshire
(Check out my latest construction project: Swimming pool by Swan Pools)