| 1 | package Data::Unixish::sort; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 8 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 8 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | #use Log::Any '$log'; | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 8 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 8 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||
| 12 | 8 | our $DATE = '2025-02-24'; # DATE | |||||||||||||||||||
| 13 | 8 | our $DIST = 'Data-Unixish'; # DIST | |||||||||||||||||||
| 14 | 8 | our $VERSION = '1.574'; # VERSION | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 8 | our %SPEC; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | $SPEC{sort} = { | ||||||||||||||||||||
| 19 | 8 | v => 1.1, | |||||||||||||||||||
| 20 | summary => 'Sort items', | ||||||||||||||||||||
| 21 | description => <<'MARKDOWN', | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | By default sort ascibetically, unless `numeric` is set to true to sort | ||||||||||||||||||||
| 24 | numerically. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | MARKDOWN | ||||||||||||||||||||
| 27 | args => { | ||||||||||||||||||||
| 28 | %common_args, | ||||||||||||||||||||
| 29 | numeric => { | ||||||||||||||||||||
| 30 | summary => 'Whether to sort numerically', | ||||||||||||||||||||
| 31 | schema=>[bool => {default=>0}], | ||||||||||||||||||||
| 32 | cmdline_aliases => { n=>{} }, | ||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||
| 34 | reverse => { | ||||||||||||||||||||
| 35 | summary => 'Whether to reverse sort result', | ||||||||||||||||||||
| 36 | schema=>[bool => {default=>0}], | ||||||||||||||||||||
| 37 | cmdline_aliases => { r=>{} }, | ||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||
| 39 | ci => { | ||||||||||||||||||||
| 40 | summary => 'Whether to ignore case', | ||||||||||||||||||||
| 41 | schema=>[bool => {default=>0}], | ||||||||||||||||||||
| 42 | cmdline_aliases => { i=>{} }, | ||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||
| 44 | random => { | ||||||||||||||||||||
| 45 | summary => 'Whether to sort by random', | ||||||||||||||||||||
| 46 | schema=>[bool => {default=>0}], | ||||||||||||||||||||
| 47 | cmdline_aliases => { R=>{} }, | ||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | key_element => { | ||||||||||||||||||||
| 51 | summary => 'Sort using an array element', | ||||||||||||||||||||
| 52 | schema => 'uint*', | ||||||||||||||||||||
| 53 | description => <<'MARKDOWN', | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | If specified, `sort` will assume the item is an array and will sort using the | ||||||||||||||||||||
| 56 | <key_element>'th element (zero-based) as key. If an item turns out to not be an | ||||||||||||||||||||
| 57 | array, the item itself is used as key. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | MARKDOWN | ||||||||||||||||||||
| 60 | }, | ||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||
| 62 | tags => [qw/ordering/], | ||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||
| 64 | 34 | ▶ | sub sort { | ||||||||||||||||||
|
Subroutine
sort: called CC=13 SCAR=26.4
Pod
documented
| |||||||||||||||||||||
| 65 | 34 | my %args = @_; | |||||||||||||||||||
| 66 | 34 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||||||||||||
| 67 | 34 | my $numeric = $args{numeric}; | |||||||||||||||||||
| 68 | 34 | ▶ | my $reverse = $args{reverse} ? -1 : 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 34 | my $ci = $args{ci}; | |||||||||||||||||||
| 70 | 34 | my $random = $args{random}; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 8 | ▶ | no warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 73 | 34 | my @buf; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # special case | ||||||||||||||||||||
| 76 | 34 | ▶ | if ($random) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | require List::Util; | |||||||||||||||||||
| 78 | 0 | ▶ | while (my ($index, $item) = each @$in) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | push @buf, $item; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 0 | push @$out, $_ for (List::Util::shuffle(@buf)); | |||||||||||||||||||
| 82 | 0 | return [200, "OK"]; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 34 | ▶ | while (my ($index, $item) = each @$in) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 122 | my $key; | |||||||||||||||||||
| 87 | 122 | ▶ | if (defined $args{key_element}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 3 | ▶ | $key = ref $item eq 'ARRAY' ? ($item->[$args{key_element}] // '') : $item; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$item[$args{"key_element"}] // ""
MC/DC0%0/1
$$item[$args{"key_element"}] // ""
$$item[$args{"key_element"}]
Truth table50%1/2
A
A= $$item[$args{"key_element"}]
| |||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||
| 90 | 119 | $key = $item; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | 122 | ▶ | $key = lc($key) if $ci; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | # XXX: optimize: when !ci && !key_element, just use $item as $key so no | ||||||||||||||||||||
| 94 | # need to produce a separate $key | ||||||||||||||||||||
| 95 | 122 | ▶ | push @buf, [$item, $key, $numeric ? $key+0 : undef]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 34 | my $sortsub; | |||||||||||||||||||
| 99 | 34 | ▶ | if ($numeric) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 10 | ▶ | $sortsub = sub { $reverse * ( | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Condition66%2/3
$Data::Unixish::sort::a->[2] <=> $Data::Unixish::sort::b->[2] || $Data::Unixish::sort::a->[1] cmp $Data::Unixish::sort::b->[1]
MC/DC0%0/2
$Data::Unixish::sort::a->[2] <=> $Data::Unixish::sort::b->[2] || $Data::Unixish::sort::a->[1] cmp $Data::Unixish::sort::b->[1]
$Data::Unixish::sort::a->[2] <=> $Data::Unixish::sort::b->[2]
$Data::Unixish::sort::a->[1] cmp $Data::Unixish::sort::b->[1]
Truth table66%2/3
A || B
A= $Data::Unixish::sort::a->[2] <=> $Data::Unixish::sort::b->[2]B= $Data::Unixish::sort::a->[1] cmp $Data::Unixish::sort::b->[1]
| |||||||||||||||||||||
| 101 | 2 | ($a->[2] <=> $b->[2]) || ($a->[1] cmp $b->[1]) ) }; | |||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||
| 103 | 125 | ▶ | $sortsub = sub { $reverse * ( | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 104 | 32 | ($a->[1] cmp $b->[1]) ) }; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | 34 | @buf = sort $sortsub @buf; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 34 | push @$out, $_->[0] for @buf; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 34 | [200, "OK"]; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 8 | 1; | |||||||||||||||||||
| 114 | # ABSTRACT: Sort items | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||