File Coverage

blib/lib/Sort/Sub/record_by_order.pm
Criterion Covered Total %
statement 18 21 85.7
branch 4 6 66.6
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 28 36 77.7


line stmt bran cond sub pod time code
1             package Sort::Sub::record_by_order;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-05-25'; # DATE
5             our $DIST = 'Sort-Sub'; # DIST
6             our $VERSION = '0.120'; # VERSION
7              
8 1     1   20 use 5.010001;
  1         4  
9 1     1   5 use strict;
  1         2  
  1         21  
10 1     1   5 use warnings;
  1         2  
  1         88  
11              
12             sub meta {
13             return {
14 0     0 0 0 v => 1,
15             compares_record => 1,
16             };
17             }
18              
19             sub gen_sorter {
20 3     3 0 7 my ($is_reverse, $is_ci) = @_;
21              
22             sub {
23 1     1   7 no strict 'refs';
  1         2  
  1         157  
24              
25 12     12   21 my $caller = caller();
26 12 50       23 my $a = @_ ? $_[0] : ${"$caller\::a"};
  0         0  
27 12 50       18 my $b = @_ ? $_[1] : ${"$caller\::b"};
  0         0  
28              
29 12         17 my $cmp = $a->[1] <=> $b->[1];
30 12 100       31 $is_reverse ? -1*$cmp : $cmp;
31 3         17 };
32             }
33              
34             1;
35             # ABSTRACT:
36              
37             __END__
38              
39             =pod
40              
41             =encoding UTF-8
42              
43             =head1 NAME
44              
45             Sort::Sub::record_by_order
46              
47             =head1 VERSION
48              
49             This document describes version 0.120 of Sort::Sub::record_by_order (from Perl distribution Sort-Sub), released on 2020-05-25.
50              
51             =head1 SYNOPSIS
52              
53             Generate sorter (accessed as variable) via L<Sort::Sub> import:
54              
55             use Sort::Sub '$record_by_order'; # use '$record_by_order<i>' for case-insensitive sorting, '$record_by_order<r>' for reverse sorting
56             my @sorted = sort $record_by_order ('item', ...);
57              
58             Generate sorter (accessed as subroutine):
59              
60             use Sort::Sub 'record_by_order<ir>';
61             my @sorted = sort {record_by_order} ('item', ...);
62              
63             Generate directly without Sort::Sub:
64              
65             use Sort::Sub::record_by_order;
66             my $sorter = Sort::Sub::record_by_order::gen_sorter(
67             ci => 1, # default 0, set 1 to sort case-insensitively
68             reverse => 1, # default 0, set 1 to sort in reverse order
69             );
70             my @sorted = sort $sorter ('item', ...);
71              
72             Use in shell/CLI with L<sortsub> (from L<App::sortsub>):
73              
74             % some-cmd | sortsub record_by_order
75             % some-cmd | sortsub record_by_order --ignore-case -r
76              
77             =head1 DESCRIPTION
78              
79             Sort by the order of records. This sorter expects C<$a> and C<$b> to be records
80             of:
81              
82             [$data, $order]
83              
84             instead of just:
85              
86             $data
87              
88             It then performs:
89              
90             $a->[1] <=> $b->[1]
91              
92             =for Pod::Coverage ^(gen_sorter|meta)$
93              
94             =head1 HOMEPAGE
95              
96             Please visit the project's homepage at L<https://metacpan.org/release/Sort-Sub>.
97              
98             =head1 SOURCE
99              
100             Source repository is at L<https://github.com/perlancar/perl-Sort-Sub>.
101              
102             =head1 BUGS
103              
104             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sort-Sub>
105              
106             When submitting a bug or request, please include a test-file or a
107             patch to an existing test-file that illustrates the bug or desired
108             feature.
109              
110             =head1 SEE ALSO
111              
112             L<Sort::Sub::record_by_reverse_order>
113              
114             L<Sort::Sub>
115              
116             =head1 AUTHOR
117              
118             perlancar <perlancar@cpan.org>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2020, 2019, 2018, 2016, 2015 by perlancar@cpan.org.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut