1Sort::Key::Maker(3) User Contributed Perl Documentation Sort::Key::Maker(3)
2
3
4
6 Sort::Key::Maker - multi-key sorter creator
7
9 # create a function that sorts strings by length:
10 use Sort::Key::Maker sort_by_length => sub { length $_}, qw(integer);
11
12 # create a multi-key sort function;
13 # first key is integer sorted in descending order,
14 # second key is a string in default (ascending) order:
15 use Sort::Key::Maker ri_s_keysort => qw(-integer string);
16
17 # some sample data...
18 my @foo = qw(foo bar t too tood mama);
19
20 # and now, use the sorter functions previously made:
21
22 # get the values on @foo sorted by length:
23 my @sorted = sort_by_length @foo;
24
25 # sort @foo inplace by its length and then by its value:
26 ri_s_keysort_inplace { length $_, $_ } @foo;
27
29 Sort::Key::Maker is a pragmatic module that provides an easy to use
30 interface to Sort::Key multi-key sorting functionality.
31
32 It creates multi-key sorting functions on the fly for any key type
33 combination and exports them to the caller package.
34
35 The key types natively accepted are:
36
37 string, str, locale, loc, integer, int,
38 unsigned_integer, uint, number, num
39
40 and support for other types can be added via Sort::Key::Register (or
41 also via Sort::Key::register_type()).
42
43 USAGE
44 use Sort::Key::Maker foo_sort => @keys;
45 exports two subroutines to the caller package: "foo_sort (&@)" and
46 "foo_sort_inplace (&\@)".
47
48 Those two subroutines require a sub reference as their first
49 argument and then respectively, the list to be sorted or an array.
50
51 For instance:
52
53 use Sort::Key::Maker bar_sort => qw(int int str);
54
55 @bar=qw(doo tomo 45s tio);
56 @sorted = bar_sort { unpack "CCs", $_ } @bar;
57 # or sorting @bar inplace
58 bar_sort_inplace { unpack "CCs", $_ } @bar;
59
60 use Sort::Key::Maker foo_sort => \&genmultikey, @keys;
61 when the first argument after the sorter name is a reference to a
62 subroutine it is used as the multi-key extraction function. The
63 generated sorter functions doesn't require neither accept one,
64 i.e.:
65
66 use Sort::Key::Maker sort_by_length => sub { length $_ }, 'int';
67 my @sorted = sort_by_length qw(foo goo h mama picasso);
68
70 Sort::Key, Sort::Key::Register.
71
72 Sort::Maker also available from CPAN provides similar functionality.
73
75 Salvador Fandiño, <sfandino@yahoo.com>
76
78 Copyright (C) 2005, 2014 by Salvador Fandiño
79
80 This library is free software; you can redistribute it and/or modify it
81 under the same terms as Perl itself, either Perl version 5.8.4 or, at
82 your option, any later version of Perl 5 you may have available.
83
84
85
86perl v5.38.0 2023-07-21 Sort::Key::Maker(3)