NAME
Sort::Key::Natural - fast natural sorting
SYNOPSIS
use Sort::Key::Natural 'natkeysort';
my @data = qw(foo1 foo23 foo6 bar12 bar1
foo bar2 bar-45 b-a-r-45);
my @sorted = natkeysort { $_ } @data;
print "@sorted\n";
# prints:
# b-a-r-45 bar1 bar2 bar12 bar-45 foo foo1 foo6 foo23
DESCRIPTION
This module extends the Sort::Key family of modules to support natural sorting.
Under natural sorting, strings are splitted in word and number boundaries, and the resulting substrings are compared as follows:
numeric substrings are compared numerically
alphabetic substrings are compared lexically
numeric substrings come always before alphabetic substrings
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.
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 multikey sorter i_rnat_keysort accepting two keys, the first to be compared as an integer and the second in natural descending order.
FUNCTIONS
the functions that can be imported from this module are:
- natkeysort { CALC_KEY($_) } @data;
-
returns the elements on
@arraynaturally sorted by the key resulting from applyingCALC_KEYto them. - rnatkeysort { CALC_KEY($_) } @data;
-
is similar to
natkeysortbut sorting the elements on descending order. - natkeysort_inplace { CALC_KEY($_) } @data;
- rnatkeysort_inplace { CALC_KEY($_) } @data;
-
these functions are similar to
natsortkeyandrnatsortkeybut sort the array@datain place. - mkkey_natural $key
-
transforms key
$keyin a way that when sorted with Sort::Key keysort results in a natural sort. If the argument$keyis not provided it defaults to$_
SEE ALSO
Other module providing similar functionality is Sort::Natural.
COPYRIGHT AND LICENSE
Copyright (C) 2006 by Salvador Fandiño, <sfandino@yahoo.com>.
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.