File Coverage

blib/lib/Sort/Sub/by_num_then_ascii.pm
Criterion Covered Total %
statement 28 29 96.5
branch 12 14 85.7
condition n/a
subroutine 6 7 85.7
pod 0 2 0.0
total 46 52 88.4


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