List:Internals« Previous MessageNext Message »
From:Benjamin Pflugmann Date:June 27 2001 11:56am
Subject:Re: natural sort
View as plain text  

On Wed, Jun 27, 2001 at 01:46:05PM +0300, sinisa@stripped wrote:
> Brannon King wrote:
> >Any plans for a natural sort? Something to the effect of 
> >
> >ORDER BY [~~~~~~~~~] NATURAL ASC
> >
> >or 
> >
> >ORDER BY [~~~~~~~~~] NATASC
> >
> >etc.
> >
> >That would be a lot easier than bringing the data back to PHP and then running
> their natsort()
> Can you explain in more detail what do you mean by natural sort ?

I digged a bit on (for the first time ;-) and found the following: 

which for the algorithm refers to

which seems to be not reachable at the moment unfortunatly, but Google
has a cached page:

The description follows:
Strings are sorted as usual, except that decimal integer substrings
are compared on their numeric value. For example,

        a < a0 < a1 < a1a < a1b < a2 < a10 < a20

Strings can contain several number parts:

        x2-g8 < x2-y7 < x2-y08 < x8-y8 

in which case numeric fields are separated by nonnumeric characters.
Leading spaces are ignored. This works very well for IP addresses from
log files, for example.

Numeric substrings with leading zeros are considered to be fractional:

        1.001 < 1.002 < 1.010 < 1.02 < 1.1 < 1.3

Performance is linear: each character of the string is scanned at most
once, and only as many characters as necessary to decide are
Related Work

POSIX sort(1) has the -n option to sort numbers, but this doesn't work
if there is a non-numeric prefix.

GNU ls(1) has the --sort=version option, which works the same way.

Stuart Cheshire's Macintosh program to do natural ordering. I already
had the idea of this routine, but borrowed the term ``natural
sort''. Article about this in a Mac magazine.

Hope this helps,


natural sortBrannon King27 Jun
  • Re: natural sortSinisa Milivojevic27 Jun
    • Re: natural sortBenjamin Pflugmann27 Jun