sub my_sort { my ($c,$d,$type) = @_; $c = lc($c); $d = lc($d); # When sorting lists of files, I want the index file to always come first. if ($c =~ /^index\./) { return -1; } elsif ($d =~ /^index\./) { return 1; } else { if (!$type || $type =~ /article/) { # This is the default sorting method. # Written with the help of kent/n in #perl on freenode. for ($c, $d) { s/<.+?>//g; # Strip out any html tags. s/\s*\b(A|a|An|an|The|the)(_|\s)//xi; # Strip off leading articles (in English). decode_entities($_); } if ( $c =~/^((\d|,|\.)+)(.*)$/ && $d =~ /^((\d|,|\.)+)(.*)$/) { # Get the leading number. (my $e = $c) =~ s/^((\d|,|\.)+)(.*)$/$1/; (my $f = $d) =~ s/^((\d|,|\.)+)(.*)$/$1/; # Take any commas out of the number. s/,//g for ($e,$f); # Get the remaining parts of the string. (my $g = $c) =~ s/^((\d|,|\.)+)(.*)$/$3/; (my $h = $d) =~ s/^((\d|,|\.)+)(.*)$/$3/; # First compare the numbers, then compare the remaining parts of the string. $e <=> $f || $g cmp $h } else { $c cmp $d; } } elsif ($type =~ /name/) { # When I sort by name I prefer lastname firstname. # I have not yet written this to account for Sr., Jr., or Roman numerals after the last name. for ($c,$d) { s/\|.+$//; $_ = join(' ', (reverse split(/(?:_|\s)(?=[^_\s]+$)/, $_,2))) if $_ !~ /^_/; s/^_//; s/^(A|a|An|an|The|the)(_|\s)//; } return $c cmp $d; } else { die(qq($type is not valid in the sort.)); } } }