| Module Info
| Add a review of Sort-Naturally
Five years ago I wrote:
The results of Sort::Naturally were not what I expected. For example, "AZZZ3827Q" sorts ahead of "A1".
I don't believe the natural sorting definition used here is the one most commonly used, and although the author explains his definition, I don't believe it's correct. Other natural sorting algorithms (for example the one in the Macintosh Finder, or Sort::Key::Naturally) do it the other way: they divide the string into alphabetic and numeric components and then compare the components, rather than comparing individual characters.
This is one of those modules that does only one thing and does it well: compare numeric substrings as numbers, and characters as characters. You "use" it, replace "sort" with "nsort", and it works like a charm. Nice!
Aaron wrote in his review that 'The results of Sort::Naturally were not what I expected. For example, "AZZZ3827Q" sorts ahead of "A1".'
The rationale for this is documented on the module's POD:
"When comparing two strings, where a numeric substring in one place is not up against a numeric substring in another, the non-numeric always comes first."
Even the synopsis code already presents this to the potential user, ordering elements as:
qw(9x 14 foo fooa foolio Foolio foo12 foo12a Foo12a foo12z foo13a)
So I really don't understand the poor rating. That said, the documentation for this module *can* be a little too confusing for such a simple purpose and contains a few typos. There is even (by the time of this writing) a two-year old ticket regarding them, which is why I gave this module 4 stars for documentation.
I used this to sort network device interface names (GigabitEthernet9/2, ge-2/1/0.432, A24, etc.) and it sorts just how I would like it and saved me a lot of hassle.
Sort::Naturally helped me get rid of about 25 lines of hard-to-read code sorting complex alphanumeric strings. nsort worked perfectly out of the box, without my needing to worry about configuration or special options.
A very easy to use module that does what you would naturally consider right ( naturally meaning what you most likely have been taught to do). Don't bother writing your own sorting if this one fits the bill.
A look at the code shows a strange construct but I guess the author had good reasons (speed?). Anyway I'm interrested in how to use it not how it is build and using it is simple.