Gossamer Forum
Home : Products : Links 2.0 : Customization :

Sortby # if hits

Quote Reply
Sortby # if hits
Can anyone tell me how to modify sub: build_sorthit in db.pl to sort by the # of hits, from greatest to least?

Ian Treibick

P.S. this is what I have:

my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, %isnew, %iscool, $hit, $i, @sorted);

for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
if ($unsorted[$db_isnew + ($i * ($#db_cols+1))] eq "Yes") { $isnew{$i} = 1; }
if ($unsorted[$db_ispop + ($i * ($#db_cols+1))] eq "Yes") { $iscool{$i} = 1; }
}
foreach $hit (sort {
if ($isnew{$b} and !$isnew{$a}) { return 1; }
if ($isnew{$a} and !$isnew{$b}) { return -1; }
if ($iscool{$b} and !$iscool{$a}) { return 1; }
if ($iscool{$a} and !$iscool{$b}) { return -1; }
if ($isnew{$a} and $isnew{$b}) { return lc($sortby{$a}) cmp lc($sortby{$b}); }
if ($iscool{$a} and $iscool{$b}) { return lc($sortby{$a}) cmp lc($sortby{$b}); }
return lc($sortby{$a}) cmp lc($sortby{$b});
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}