I have this snippet, which sorts values, e.g. Title2 2005; Title2 1998; Title3 1994; etc by decade and is working flawlessly. But I need to add additional sorting of the already sorted decades – first numerically descending and then alphabetically ascending. If I use the second_sort subroutine I get an error ‘Can’t locate object method via package’. Do I have a small error in my code or my logic is totally wrong and I should seek another approach.
Thanks.
use autodie;
use strict;
use warnings;
use HTML::TreeBuilder;
my %sort = ();
push (@{$sort{$decade}}, $t );
for my $decade ( sort { $b <=> $a } keys %sort ) {
my @td = map { $_->as_HTML("<>&","\t",{}) } second_sort( @{$sort{$decade}} );
}
sub second_sort {
my @sorted = map { @$_ }
sort { $b->[1] <=> $a->[1] || lc $a->[0] cmp lc $b->[0] }
map {
my ($title, $year) = ($_->as_trimmed_text =~ /(.*?)\((\d+)\).*/);
[$title, $year];
} @_;
return @sorted;
}
It seems that you are missing the use of the HTML::Element module to provide ‘as_trimmed_text’.