use Sort::Key::Natural qw(natsort); my @data = qw(foo1 foo23 foo6 bar12 bar1 foo bar2 bar-45 foomatic b-a-r-45); my @sorted = natsort @data; print "@sorted\n"; # prints: # b-a-r-45 bar1 bar2 bar12 bar-45 foo foo1 foo6 foo23 foomatic use Sort::Key::Natural qw(natkeysort); my @objects = (...); my @sorted = natkeysort { $_->get_id } @objects;
Under natural sorting, strings are split at word and number boundaries, and the resulting substrings are compared as follows:
Spaces, symbols and non-printable characters are only considered for splitting the string into its parts but not for sorting. For instance "foo-bar-42" is broken in three substrings "foo", "bar" and 42 and after that the dashes are ignored.
Note, that the sorting is case sensitive. To do a case insensitive sort you have to convert the keys explicitly:
my @sorted = natkeysort { lc $_ } @data
Also, once this module is loaded, the new type "natural" (or "nat") will be available from Sort::Key::Maker. For instance:
use Sort::Key::Natural; use Sort::Key::Maker i_rnat_keysort => qw(integer -natural);
creates a multi-key sorter "i_rnat_keysort" accepting two keys, the first to be compared as an integer and the second in natural descending order.
There is also an alternative set of natural sorting functions that recognize floating point numbers. They use the key type "natwf" (abbreviation of "natural_with_floats").
If the argument $key is not provided it defaults to $_.
In this context a floating point number is a string matching the regular expression "/[+\-]?\d+(\.\d*)?/". Note that numbers with an exponent part (i.e. "1.12E-12") are not recognized as such.
Note also that numbers without an integer part (i.e. .2 or "-.12") are not supported either.
Other module providing similar functionality is Sort::Naturally.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.