1Sort::Key::Natural(3) User Contributed Perl DocumentationSort::Key::Natural(3)
2
3
4
6 Sort::Key::Natural - fast natural sorting
7
9 use Sort::Key::Natural qw(natsort);
10
11 my @data = qw(foo1 foo23 foo6 bar12 bar1
12 foo bar2 bar-45 foomatic b-a-r-45);
13
14 my @sorted = natsort @data;
15
16 print "@sorted\n";
17 # prints:
18 # b-a-r-45 bar1 bar2 bar12 bar-45 foo foo1 foo6 foo23 foomatic
19
20 use Sort::Key::Natural qw(natkeysort);
21
22 my @objects = (...);
23 my @sorted = natkeysort { $_->get_id } @objects;
24
26 This module extends the Sort::Key family of modules to support natural
27 sorting.
28
29 Under natural sorting, strings are splitted at word and number
30 boundaries, and the resulting substrings are compared as follows:
31
32 · numeric substrings are compared numerically
33
34 · alphabetic substrings are compared lexically
35
36 · numeric substrings come always before alphabetic substrings
37
38 Spaces, symbols and non-printable characters are only considered for
39 splitting the string into its parts but not for sorting. For instance
40 "foo-bar-42" is broken in three substrings "foo", "bar" and 42 and
41 after that the dashes are ignored.
42
43 Also, once this module is loaded, the new type "natural" (or "nat")
44 will be available from Sort::Key::Maker. For instance:
45
46 use Sort::Key::Natural;
47 use Sort::Key::Maker i_rnat_keysort => qw(integer -natural);
48
49 creates a multikey sorter "i_rnat_keysort" accepting two keys, the
50 first to be compared as an integer and the second in natural descending
51 order.
52
53 FUNCTIONS
54 the functions that can be imported from this module are:
55
56 natsort @data
57 returns the elements of @data sorted in natural order.
58
59 rnatsort @data
60 returns the elements of @data sorted in natural descending order.
61
62 natkeysort { CALC_KEY($_) } @data
63 returns the elements on @array naturally sorted by the keys
64 resulting from applying them "CALC_KEY".
65
66 rnatkeysort { CALC_KEY($_) } @data
67 is similar to "natkeysort" but sorts the elements in descending
68 order.
69
70 natsort_inplace @data
71 rnatsort_inplace @data
72 natkeysort_inplace { CALC_KEY($_) } @data
73 rnatkeysort_inplace { CALC_KEY($_) } @data
74 these functions are similar respectively to "natsort", "rnatsort",
75 "natsortkey" and "rnatsortkey", but they sort the array @data in
76 place.
77
78 mkkey_natural $key
79 transforms key $key in a way that when sorted with Sort::Key
80 keysort results in a natural sort. If the argument $key is not
81 provided it defaults to $_
82
84 Sort::Key, Sort::Key::Maker.
85
86 Other module providing similar functionality is Sort::Naturally.
87
89 Copyright (C) 2006 by Salvador Fandin~o, <sfandino@yahoo.com>.
90
91 This library is free software; you can redistribute it and/or modify it
92 under the same terms as Perl itself, either Perl version 5.8.4 or, at
93 your option, any later version of Perl 5 you may have available.
94
95
96
97perl v5.12.0 2006-05-08 Sort::Key::Natural(3)