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 split at word and number boundaries,
30 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 Note, that the sorting is case sensitive. To do a case insensitive sort
44 you have to convert the keys explicitly:
45
46 my @sorted = natkeysort { lc $_ } @data
47
48 Also, once this module is loaded, the new type "natural" (or "nat")
49 will be available from Sort::Key::Maker. For instance:
50
51 use Sort::Key::Natural;
52 use Sort::Key::Maker i_rnat_keysort => qw(integer -natural);
53
54 creates a multi-key sorter "i_rnat_keysort" accepting two keys, the
55 first to be compared as an integer and the second in natural descending
56 order.
57
58 There is also an alternative set of natural sorting functions that
59 recognize floating point numbers. They use the key type "natwf"
60 (abbreviation of "natural_with_floats").
61
62 FUNCTIONS
63 the functions that can be imported from this module are:
64
65 natsort @data
66 returns the elements of @data sorted in natural order.
67
68 rnatsort @data
69 returns the elements of @data sorted in natural descending order.
70
71 natkeysort { CALC_KEY($_) } @data
72 returns the elements on @array naturally sorted by the keys
73 resulting from applying them "CALC_KEY".
74
75 rnatkeysort { CALC_KEY($_) } @data
76 is similar to "natkeysort" but sorts the elements in descending
77 order.
78
79 natsort_inplace @data
80 rnatsort_inplace @data
81 natkeysort_inplace { CALC_KEY($_) } @data
82 rnatkeysort_inplace { CALC_KEY($_) } @data
83 these functions are similar respectively to "natsort", "rnatsort",
84 "natsortkey" and "rnatsortkey", but they sort the array @data in
85 place.
86
87 $key = mkkey_natural $string
88 given $string, returns a key that can be compared lexicographically
89 to another key obtained in the same manner, results in the same
90 order as comparing the former strings as in the natural order.
91
92 If the argument $key is not provided it defaults to $_.
93
94 natwfsort @data
95 rnatwfsort @data
96 natwfkeysort { CALC_KEY($_) } @data
97 rnatwfkeysort { CALC_KEY($_) } @data
98 natwfsort_inplace @data
99 rnatwfsort_inplace @data
100 natwfkeysort_inplace { CALC_KEY($_) } @data
101 rnatwfkeysort_inplace { CALC_KEY($_) } @data
102 mkkey_natural_with_floats $key
103 this ugly named set of functions perform in the same way as its
104 s/natwf/nat/ counterpart with the difference that they honor
105 floating point numbers embedded inside the strings.
106
107 In this context a floating point number is a string matching the
108 regular expression "/[+\-]?\d+(\.\d*)?/". Note that numbers with an
109 exponent part (i.e. 1.12E-12) are not recognized as such.
110
111 Note also that numbers without an integer part (i.e. .2 or -.12)
112 are not supported either.
113
115 Sort::Key, Sort::Key::Maker.
116
117 Other module providing similar functionality is Sort::Naturally.
118
120 Copyright (C) 2006, 2012, 2014 by Salvador Fandiño,
121 <sfandino@yahoo.com>.
122
123 This library is free software; you can redistribute it and/or modify it
124 under the same terms as Perl itself, either Perl version 5.8.4 or, at
125 your option, any later version of Perl 5 you may have available.
126
127
128
129perl v5.38.0 2023-07-21 Sort::Key::Natural(3)