| 1 | package App::WRT::Sort; | ||||||||||
| 2 | |||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 10 | ▶ | use 5.10.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 10 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 10 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 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<https://en.wikipedia.org/wiki/Schwartzian_transform> | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | sub sort_entries { | ||||||||
|
Subroutine
sort_entries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 1 | my (@entries) = @_; | |||||||||
| 46 | 19 | return map { $_->[0] } | |||||||||
| 47 | 57 | sort { $a->[1] cmp $b->[1] } | |||||||||
| 48 | 1 | map { [$_, sortable_from_entry($_)] } | |||||||||
| 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 | 19 | ▶ | sub sortable_from_entry { | ||||||||
|
Subroutine
sortable_from_entry: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 62 | 19 | my ($entry) = @_; | |||||||||
| 63 | |||||||||||
| 64 | my @parts = map { | ||||||||||
| 65 | |||||||||||
| 66 | 19 | my $padded; | |||||||||
| 67 | 43 | ▶ | if (m/^\d+$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | # There's a year 100k bug here, but I guess I'll cross that bridge when I | ||||||||||
| 69 | # come to it: | ||||||||||
| 70 | 27 | $padded = sprintf("%05d", $_); | |||||||||
| 71 | } else { | ||||||||||
| 72 | 16 | $padded = $_; | |||||||||
| 73 | } | ||||||||||
| 74 | 43 | $padded; | |||||||||
| 75 | |||||||||||
| 76 | } split '/', $entry; | ||||||||||
| 77 | |||||||||||
| 78 | 19 | return join '', @parts; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | =back | ||||||||||
| 82 | |||||||||||
| 83 | =cut | ||||||||||
| 84 | |||||||||||
| 85 | 10 | 1; | |||||||||