File Coverage

blib/lib/App/WRT/Sort.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1             package App::WRT::Sort;
2              
3 10     10   110093 use strict;
  10         27  
  10         340  
4 10     10   52 use warnings;
  10         16  
  10         246  
5 10     10   98 use 5.10.0;
  10         37  
6              
7 10     10   60 use Carp;
  10         16  
  10         666  
8              
9 10     10   64 use base qw(Exporter);
  10         14  
  10         3389  
10             our @EXPORT_OK = qw(sort_entries);
11              
12             =pod
13              
14             =head1 NAME
15              
16             App::WRT::Sort - functions for sorting wrt entry lists
17              
18             =head1 SYNOPSIS
19              
20             use App::WRT::Sort qw(sort_entries);
21             my (@sorted) = sort_entries(@unsorted);
22              
23             =head1 DESCRIPTION
24              
25             This makes an effort to sort a list of entries, which may include both simple
26             dates and names, or a combination thereof, reasonably.
27              
28             The main goal is to have dates sorted in order, followed by alphanumeric names.
29              
30             =head1 FUNCTIONS
31              
32             =over
33              
34             =item sort_entries(@entries)
35              
36             Sort a list of entries by converting them to an array with an easily sortable
37             string format as the second value, sorting these arrayrefs by that value, and
38             then re-mapping them to the original values.
39              
40             See here: L
41              
42             =cut
43              
44             sub sort_entries {
45 92     92 1 359 my (@entries) = @_;
46 195         481 return map { $_->[0] }
47 189         289 sort { $a->[1] cmp $b->[1] }
48 92         157 map { [$_, sortable_from_entry($_)] }
  195         314  
49             @entries;
50             }
51              
52             =item sortable_from_entry($entry)
53              
54             Get a sortable string value that does (more or less) what we want.
55              
56             In this case, it pads numeric components of the path out to 5 leading 0s and
57             leaves everything else alone.
58              
59             =cut
60              
61             sub sortable_from_entry {
62 195     195 1 268 my ($entry) = @_;
63              
64             my @parts = map {
65              
66 195         398 my $padded;
  351         375  
67 351 100       872 if (m/^\d+$/) {
68             # There's a year 100k bug here, but I guess I'll cross that bridge when I
69             # come to it:
70 291         719 $padded = sprintf("%05d", $_);
71             } else {
72 60         90 $padded = $_;
73             }
74 351         656 $padded;
75              
76             } split '/', $entry;
77              
78 195         733 return join '', @parts;
79             }
80              
81             =back
82              
83             =cut
84              
85             1;