line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hub::Perl::Sort; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
3
|
1
|
|
|
1
|
|
6
|
use Hub qw/:lib/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
our $VERSION = '4.00043'; |
5
|
|
|
|
|
|
|
our @EXPORT = qw//; |
6
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
7
|
|
|
|
|
|
|
anon_sort |
8
|
|
|
|
|
|
|
keydepth_sort |
9
|
|
|
|
|
|
|
/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
12
|
|
|
|
|
|
|
# anon_sort - Anonymous value sort |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# anon_sort [OPTIONS], \ARRAY |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# OPTIONS: |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
# -on keyname Only sort subhashes with this keyname. |
19
|
|
|
|
|
|
|
# -cmp (<=>|cmp) Comparison type (default is 'cmp'.) |
20
|
|
|
|
|
|
|
# -asr (0|1) Return a reference to the result array. |
21
|
|
|
|
|
|
|
# -modify (0|1) Modify the provided array. |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
24
|
|
|
|
|
|
|
=test(match) # Simple (alphabetical) sort |
25
|
|
|
|
|
|
|
my @months = qw/Jan Feb Mar/; |
26
|
|
|
|
|
|
|
my @sorted = anon_sort( \@months ); |
27
|
|
|
|
|
|
|
return join ',', @sorted; |
28
|
|
|
|
|
|
|
=result |
29
|
|
|
|
|
|
|
Feb,Jan,Mar |
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub anon_sort { |
34
|
0
|
|
|
0
|
1
|
|
my $opts = { |
35
|
|
|
|
|
|
|
'cmp' => 'cmp', |
36
|
|
|
|
|
|
|
'on' => '', |
37
|
|
|
|
|
|
|
'asr' => 0, |
38
|
|
|
|
|
|
|
'modify' => 0, |
39
|
|
|
|
|
|
|
}; |
40
|
0
|
|
|
|
|
|
Hub::opts( \@_, $opts ); |
41
|
0
|
|
|
|
|
|
my @all = (); |
42
|
0
|
|
|
|
|
|
while( @_ ) { |
43
|
0
|
|
|
|
|
|
my @result = (); |
44
|
0
|
|
|
|
|
|
my $x = shift; |
45
|
0
|
|
|
|
|
|
Hub::expect( ARRAY => $x, '-back=2' ); |
46
|
0
|
|
|
|
|
|
my $list = []; |
47
|
0
|
0
|
|
|
|
|
if( $$opts{'on'} ) { |
48
|
0
|
0
|
|
|
|
|
map { Hub::check( '-ref=HASH', $_ ) and push @$list, $_ } @$x; |
|
0
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
} else { |
50
|
0
|
|
|
|
|
|
$list = $x; |
51
|
|
|
|
|
|
|
}#if |
52
|
0
|
0
|
|
|
|
|
if( $$opts{'on'} ) { |
53
|
0
|
|
|
|
|
|
@result = sort { |
54
|
0
|
|
|
|
|
|
Hub::compare( $$opts{'cmp'}, |
55
|
|
|
|
|
|
|
Hub::getv($a, $$opts{"on"}), Hub::getv($b, $$opts{"on"})); |
56
|
|
|
|
|
|
|
} @$list; |
57
|
|
|
|
|
|
|
} else { |
58
|
0
|
|
|
|
|
|
@result = sort { |
59
|
0
|
|
|
|
|
|
Hub::compare( $$opts{'cmp'}, |
60
|
|
|
|
|
|
|
Hub::bestof( $a, -1 ), Hub::bestof( $b, -1 ) ); |
61
|
|
|
|
|
|
|
} @$list; |
62
|
|
|
|
|
|
|
}#if |
63
|
0
|
0
|
|
|
|
|
if( $$opts{'modify'} ) { |
64
|
0
|
|
|
|
|
|
@$x = @result; |
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
|
push @all, \@result; |
67
|
|
|
|
|
|
|
}#if |
68
|
|
|
|
|
|
|
}#while |
69
|
0
|
0
|
|
|
|
|
@all == 1 and @all = @{ pop @all }; |
|
0
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
return $$opts{'asr'} ? \@all : @all; |
71
|
|
|
|
|
|
|
}#anon_sort |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
74
|
|
|
|
|
|
|
# keydepth_sort - Sort by number of semicolons |
75
|
|
|
|
|
|
|
# keydepth_sort |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
# Sort by keydepth (for processing hashes and making sure parents don't smuther |
78
|
|
|
|
|
|
|
# their children.) |
79
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub keydepth_sort { |
82
|
0
|
|
|
0
|
1
|
|
return sort {Hub::keydepth($a) <=> Hub::keydepth($b)} @_; |
|
0
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
}#keydepth_sort |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------ |
86
|
|
|
|
|
|
|
1; |