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

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 PHP.net (for the first time ;-) and found the following:

http://www.php.net/manual/en/function.natsort.php 

which for the algorithm refers to 

http://www.linuxcare.com.au/projects/natsort/

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

http://www.google.com/search?q=cache:GyOlzOSjQss:linuxcare.com.au/projects/natsort/+natsort&hl=en

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
considered.
[...]
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,

        Benjamin.

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