Gossamer Forum
Home : Products : Links 2.0 : Discussions :

Links 1.11

Quote Reply
Links 1.11
What do I need to do to the following code below so that it lists alphabetically first. I want my what's new page to list alphabetically -

sub build_sorthit {
# --------------------------------------------------------
# This function sorts a list of links. It has been modified to sort
# new links first, then cool links, then the rest alphabetically. By modifying
# the sort function below, you can sort the links however you like (by date,
# or random, etc.).
#
my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, %isnew, %ispopular, $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") { $ispopular{$i} = 1; }
}
foreach $hit (sort {
if ($isnew{$b} and !$isnew{$a}) { return 1; }
if ($isnew{$a} and !$isnew{$b}) { return -1; }
if ($ispopular{$b} and !$ispopular{$a}) { return 1; }
if ($ispopular{$a} and !$ispopular{$b}) { return -1; }
if ($isnew{$a} and $isnew{$b}) { return lc($sortby{$a}) cmp lc($sortby{$b}); }
if ($ispopular{$a} and $ispopular{$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;
}