I rewrote my custom sort function, but I am not sure I did it the best way possible. A few sets of fresh eyeballs might see something obvious where I may have overcomplicated the function. The section I rewrote is in the the first if in the first else. If you make it as far as the elsif for sorting by name, I could use some pointers of how to account for name suffixes like Jr. or III.
Thank you for looking.
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 arti
+cles (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.));
}
}
}
Have a cookie and a very nice day!
Lady Aleena
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|