Gossamer Forum
Home : General : Perl Programming :

sorting

Quote Reply
sorting
Hi,

I've been trying to modify the sort routine, of links 2 and posted my question about something weird at the links 2 modify board a few days ago, but maybe this is a better place to get the right attention for it. In stead of cross posting I'll just put a link to it here:

http://www.gossamer-threads.com/...latest_reply;so=ASC;

Please take a look at the code to see if somebody knows what's going wrong. I tried to add a field and if its content is 'Yes' it should place the link on top of the list. What happens now is that it works, except for one or two links it still leaves on top. I guess my sorting isn't 100% foolproof, and it leaves the last (two) links to be sorted on the top in stead of putting them on their right spot as well. So, I guess I've left a gap in it... But can't fix it by myself.

Thanks,

Lex
Quote Reply
Re: [Lex] sorting In reply to
Here is the code that somehow doesn't finfish off the sorting 100% and leaves one or two links at the top that shouldn't be there. The rest is sorted just fine. So I guess the order in which the sorting takes place isn't the way it should be. Can somebody take a look please? Or tell me where I could find a solution?

Thanks a lot!

Lex

Code:
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, %iscool, $hit, $i, @sorted); for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
($unsorted[$db_priority + ($i * ($#db_cols+1))] eq "Yes") and ($priority{$i} = 1);
($unsorted[$db_isnew + ($i * ($#db_cols+1))] eq "Yes") and ($isnew{$i} = 1);
($unsorted[$db_ispop + ($i * ($#db_cols+1))] eq "Yes") and ($iscool{$i} = 1);
}
foreach $hit (sort {
($priority{$b} and !$priority{$a}) and return 1;
($priority{$a} and !$priority{$b}) and return -1;
($isnew{$b} and !$isnew{$a}) and return 1;
($isnew{$a} and !$isnew{$b}) and return -1;
($iscool{$b} and !$iscool{$a}) and return 1;
($iscool{$a} and !$iscool{$b}) and return -1;
($priority{$a} and $priority{$b}) and return lc($sortby{$a}) cmp lc($sortby{$b});
($isnew{$a} and $isnew{$b}) and return lc($sortby{$a}) cmp lc($sortby{$b});
($iscool{$a} and $iscool{$b}) and 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;
}
Quote Reply
Re: [Lex] sorting In reply to
I forgot to put %priority in
my (%sortby, %isnew, %iscool, %priority, $hit, $i, @sorted);
Works like a charm now!

Lex