File Coverage

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