File Coverage

blib/lib/Data/Unixish/sort.pm
Criterion Covered Total %
statement 39 44 88.6
branch 11 12 91.6
condition 2 3 66.6
subroutine 9 9 100.0
pod 1 1 100.0
total 62 69 89.8


line stmt bran cond sub pod time code
1             package Data::Unixish::sort;
2              
3 7     7   3768 use 5.010;
  7         24  
4 7     7   35 use strict;
  7         8  
  7         166  
5 7     7   357 use syntax 'each_on_array'; # to support perl < 5.12
  7         19815  
  7         43  
6 7     7   3783 use warnings;
  7         14  
  7         217  
7             #use Log::Any '$log';
8              
9 7     7   393 use Data::Unixish::Util qw(%common_args);
  7         22  
  7         1519  
10              
11             our $VERSION = '1.570'; # VERSION
12              
13             our %SPEC;
14              
15             $SPEC{sort} = {
16             v => 1.1,
17             summary => 'Sort items',
18             description => <<'_',
19              
20             By default sort ascibetically, unless `numeric` is set to true to sort
21             numerically.
22              
23             _
24             args => {
25             %common_args,
26             numeric => {
27             summary => 'Whether to sort numerically',
28             schema=>[bool => {default=>0}],
29             cmdline_aliases => { n=>{} },
30             },
31             reverse => {
32             summary => 'Whether to reverse sort result',
33             schema=>[bool => {default=>0}],
34             cmdline_aliases => { r=>{} },
35             },
36             ci => {
37             summary => 'Whether to ignore case',
38             schema=>[bool => {default=>0}],
39             cmdline_aliases => { i=>{} },
40             },
41             random => {
42             summary => 'Whether to sort by random',
43             schema=>[bool => {default=>0}],
44             cmdline_aliases => { R=>{} },
45             },
46             },
47             tags => [qw/ordering/],
48             };
49             sub sort {
50 33     33 1 231 my %args = @_;
51 33         159 my ($in, $out) = ($args{in}, $args{out});
52 33         99 my $numeric = $args{numeric};
53 33 100       116 my $reverse = $args{reverse} ? -1 : 1;
54 33         93 my $ci = $args{ci};
55 33         89 my $random = $args{random};
56              
57 7     7   43 no warnings;
  7         14  
  7         2003  
58 33         264 my @buf;
59              
60             # special case
61 33 50       110 if ($random) {
62 0         0 require List::Util;
63 0         0 while (my ($index, $item) = each @$in) {
64 0         0 push @buf, $item;
65             }
66 0         0 push @$out, $_ for (List::Util::shuffle(@buf));
67 0         0 return [200, "OK"];
68             }
69              
70 33         298 while (my ($index, $item) = each @$in) {
71 119         7193 my $rec = [$item];
72 119 100       277 push @$rec, $ci ? lc($item) : undef; # cache lowcased item
73 119 100       216 push @$rec, $numeric ? $item+0 : undef; # cache numeric item
74 119         383 push @buf, $rec;
75             }
76              
77 33         177 my $sortsub;
78 33 100       68 if ($numeric) {
79 10   66 10   29 $sortsub = sub { $reverse * (
80             ($a->[2] <=> $b->[2]) ||
81 2         8 ($ci ? ($a->[1] cmp $b->[1]) : ($a->[0] cmp $b->[0]))) };
82             } else {
83 122 100   122   406 $sortsub = sub { $reverse * (
84 31         317 $ci ? ($a->[1] cmp $b->[1]) : ($a->[0] cmp $b->[0])) };
85             }
86 33         240 @buf = sort $sortsub @buf;
87              
88 33         274 push @$out, $_->[0] for @buf;
89              
90 33         387 [200, "OK"];
91             }
92              
93             1;
94             # ABSTRACT: Sort items
95              
96             __END__