Gossamer Forum
Home : Products : Links 2.0 : Customization :

Re: Sorting Links (Yes I know...Not again!!) :-D

Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
Hi everyone,

I know, "again a sorting question..."...
I am actually trying to do exactly like Vickie did: I have added a couple of new fields in my links.def, and I want my links to be sorted by "great" (see links.def below), not "popular". I am doing exactly like posted but still I get links sorted by New (which is ok), but my "great" links are still sorted randomly in the middle of other links. Below I posted links.def and db_utils, please have a look.

Links.def

Code:
# Database Definition: LINKS
# --------------------------------------------------------
# Definition of your database file.
%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
Title => [1, 'alpha', 40, 75, 1, '', ''],
URL => [2, 'alpha', 40, 75, 1, 'http://', '^http|news|mailto|ftp'],
Date => [3, 'date', 15, 15, 1, \&get_date, ''],
Category => [4, 'alpha', 0, 150, 1, '', ''],
Description => [5, 'alpha', '40x3', 500, 0, '', ''],
'Contact Name' => [6, 'alpha', 40, 75, 1, 'OG', ''],
'Contact Email' => [7, 'alpha', 40, 75, 1, '', '.+@.+\..+'],
Hits => [8, 'numer', 10, 10, 1, '0', '\d+'],
isNew => [9, 'alpha', 0, 5, 0, 'Yes', ''],
isPopular => [10, 'alpha', 0, 5, 0, 'No', ''],
Rating => [11, 'numer', 10, 10, 1, 0, '^[\d\.]+$'],
Votes => [12, 'numer', 10, 10, 1, 0, '^\d+$'],
ReceiveMail => [13, 'alpha', 10, 10, 1, 'No', 'No|Yes'],
Review => [14, 'alpha', '40x3', 500, 0, '', ''],
AltCategories => [15, 'alpha', 0, 500, 0, '', ''],
Member => [16, 'alpha', 0, 5, 0, '', 'No|Yes'],
AVS => [17, 'alpha', 0, 5, 0, '', 'No|Yes'],
Gallery => [18, 'alpha', 0, 5, 0, '', 'No|Yes'],
Chat => [19, 'alpha', 0, 5, 0, '', 'No|Yes'],
Classifieds => [20, 'alpha', 0, 5, 0, '', 'No|Yes'],
Shopping => [21, 'alpha', 0, 5, 0, '', 'No|Yes'],
Great => [22, 'alpha', 0, 5, 0, '', 'No|Yes'],
Verygood => [23, 'alpha', 0, 5, 0, '', 'No|Yes'],
Good => [24, 'alpha', 0, 5, 0, '', 'No|Yes'],
Average => [25, 'alpha', 0, 5, 0, '', 'No|Yes'],
Poor => [26, 'alpha', 0, 5, 0, '', 'No|Yes'],
Verypoor => [27, 'alpha', 0, 5, 0, '', 'No|Yes']
);

And in my db_utils:

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, %great, $hit, $i, @sorted);

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

Is there something wrong in this code? Any help would be great!
Thank you.
Subject Author Views Date
Thread Sorting Links (Yes I know...Not again!!) :-D VickieB 7916 Apr 11, 1999, 4:32 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
phoenix 7758 Apr 11, 1999, 4:48 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
Alex 7774 Apr 11, 1999, 9:28 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
VickieB 7783 Apr 11, 1999, 9:57 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
Bobsie 7805 Apr 11, 1999, 10:57 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
VickieB 7795 Apr 12, 1999, 1:37 AM
Thread Re: Sorting Links (Yes I know...Not again!!) :-D
Alex 7778 Apr 12, 1999, 1:55 PM
Thread Re: [Alex] Sorting Links (Yes I know...Not again!!) :-D
Coffee 5759 Jan 8, 2005, 9:04 AM
Post Re: [Coffee] Sorting Links (Yes I know...Not again!!) :-D
bugsie 5376 Jun 27, 2006, 4:53 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
VickieB 7770 Apr 12, 1999, 2:46 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
kiel1 7765 Nov 24, 1999, 4:31 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
Tiny Giants 7757 Nov 28, 1999, 2:40 PM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
Tj2000 7757 Nov 29, 1999, 6:19 AM
Post Re: Sorting Links (Yes I know...Not again!!) :-D
doolittle 7786 Apr 19, 2000, 7:58 PM
Thread Re: Sorting Links (Yes I know...Not again!!) :-D
doolittle 7762 Apr 20, 2000, 4:41 AM
Thread Re: [doolittle] Sorting Links (Yes I know...Not again!!) :-D
Lex 6301 May 26, 2003, 5:53 AM
Thread Re: [Lex] Sorting Links (Yes I know...Not again!!) :-D
Lex 6253 Jun 10, 2003, 2:04 PM
Post Re: [Lex] Sorting Links (Yes I know...Not again!!) :-D
poscon 5848 Aug 24, 2004, 10:51 AM