Gossamer Forum
Home : Products : Links 2.0 : Customization :

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

Quote Reply
Sorting Links (Yes I know...Not again!!) :-D
I have spent considerable time trying to figure out how to do this (reading through the archives and trial and error) but am not having any luck.

I have added a new field to the links.cfg called Priority:

Priority => [14, 'alpha', 0, 5, 0, 'No', '']

It works very similarly to the new and popular fields.

What I would like to do, sorting links wise, is to have Priority show up first and then New and then all the rest in random order.

I can't figure out how to change the code in db_utils.pl to make this work.

Is there anyone out there that would be willing to help me out on this or point me to a thread or threads that would help me to figure it out?

Thanks so much for any help that you can give to me!
Vickie
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
I am not the best person to help you. I've just got to say LOL to your apologetic tone for asking yet another sort question. I know how you feel... I had to ask one of those, and also a dreaded category-list appearance question.

Luck!
Phoenix
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
Can you post your entire build_sort_hit subroutine so I can see how you are sorting by priority right now?

Cheers,

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

Okay you asked for it Smile Any help is much appreciated.

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, %priority, %isnew, $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_priority + ($i * ($#db_cols+1))] eq "Yes") and ($priority{$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;
($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});

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;
}

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

Take a look at http://www.gossamer-threads.com/...um3/HTML/000133.html as I believe it will do exactly what you want with one exception. You will need to reverse the two lines:

Quote:
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; }

and change the $db_ispop to $db_priority (assuming you have defined that in links.def) and $iscool to $priority. Also, you will need to change the my (%sortby, %isnew, %iscool, $hit, $i, @sorted); to remove %iscool and add in %priority.

That should do it for you. Let me know if it works out for you (I haven't actually tested it).
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
Hi Bobsie,

Thanks for taking the time to answer my question, but that routine only solves half my problem. With the replacement that you pointed me to, it makes all the links sort randomly. I would like Priority links to show up first, then new and then all the rest random.

WooHoo, I figured out how to get priority first, then new, but now I need to figure out how to randomly list the rest. I think that
Code:
(keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

needs to be changed to cause random listing of the remaining links? If it is, could someone let me know what the code needs to be changed to??


Thanks so much for bearing with me! Smile
Vickie

[This message has been edited by VickieB (edited April 12, 1999).]
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
You've got it right, just replace:

return lc($sortby{$a}) cmp lc($sortby{$b});

with:

return (rand > 0.5) ? 1 : -1;

and it should sort by priority, new, then random.

Cheers,

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

Thanks so much! I works!

I really appreciate you helping me out!
Vickie
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
is there a way to get, say site with a rating of 7 or higher sorted in there too?
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
OK, I have searched and I have looked for the priority logo in the resources section (file not found error). I found this thread and I am trying to make it work, but....

So far I added the priority line to links.def

I also added VickieB's code with Alex's changes. When I went to my admin panel I had the blank block for the priority section. What do I need to enter into the block to make it sort to the top of a list. Itried to enter priority, but it was to big. I entered "Yes" ,but it seemed to not get the site to the top either. The only thing I have succeded in doing is loosing my cool links sort and gain a random sort.

I replaced this

($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;
($priority{$a} and $priority{$b}) and return (rand > 0.5) ? 1 : -1;
($isnew{$a} and $isnew{$b}) and return (rand > 0.5) ? 1 : -1;

return (rand > 0.5) ? 1 : -1;

with this

($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 (rand > 0.5) ? 1 : -1;
($isnew{$a} and $isnew{$b}) and return (rand > 0.5) ? 1 : -1;
($iscool{$a} and $iscool{$b}) and return (rand > 0.5) ? 1 : -1;
return (rand > 0.5) ? 1 : -1;

Any Help would be appreciated.
Dale
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
Interesting... Is there a way to modify this to have each category sorted by a different order.
For example have category_A sorted from newer to oldest (all the time, not only when it's new) and category_b sorted alphabetically?
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.
Quote Reply
Re: Sorting Links (Yes I know...Not again!!) :-D In reply to
I figured out what I was doing wrong...
I hadn't defined the "# Field Number of some important fields." in links.def.
Now it works perfectly. Sorry about that.

Regards
Quote Reply
Re: [doolittle] Sorting Links (Yes I know...Not again!!) :-D In reply to
Right, I've got this working. However, in one category I still get something weird. It first gives a cool link, then a normal link, than my 'priority' link (Laila) and than the new links followed by the normal links.

You can see it here: http://www.gran-canaria-info.com/...ts/Playa_del_Ingles/

In my other categories I haven't found something like this.

I don't see where the problem orginates. Can somebody help?

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 Links (Yes I know...Not again!!) :-D In reply to
I forgot to put %priority in
my (%sortby, %isnew, %iscool, %priority, $hit, $i, @sorted);
Works like a charm now!

Lex
Quote Reply
Re: [Lex] Sorting Links (Yes I know...Not again!!) :-D In reply to
Alex & VickeyB - great job! Works like a champ! BUT,

I'm trying to add a "Priority" flag to links and am obviously missing something.

in site_html_templates i have (bold are the added lines):
sub site_html_link {
# --------------------------------------------------------
# This routine is used to display what a link should look
# like.
my %rec = @_;
# Set new and pop to either 1 or 0 for templates.
($rec{'Priority'} eq 'Yes') ? ($rec{'Priority'} = 1) : (delete $rec{'Priority'});
($rec{'isNew'} eq 'Yes') ? ($rec{'isNew'} = 1) : (delete $rec{'isNew'});
($rec{'isPopular'} eq 'Yes') ? ($rec{'isPopular'} = 1) : (delete $rec{'isPopular'});
return &load_template ('link.html', {
detailed_url => "$db_detailed_url/$rec{'ID'}$build_extension",
%rec,
%globals
});
}

in link.html i have:
<%if isNew%>
<sup class="new">New</sup>
<%endif%>
<%if isPopular%>
<small><sup class="pop">Pop</sup></small>
<%endif%>
<%if Priority%>
<sup class="priority">Priority</sup>
<%endif%>


Still does not work -- what am I missing?
-----------------------------------------------------------
Professional Search Engine Marketing by Position Concepts Professional Consulting in SEM, SEO and PPC.
Quote Reply
Re: [Alex] Sorting Links (Yes I know...Not again!!) :-D In reply to
I've got this thing working ... I however don't use hit tracking on my site so they are not sorted randomly. They are shown by their ID.

Now HOW can I make them show up randomly - I mean truely random.
Here's my build_sorthit rutine:

my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, %priority, %isnew, $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);
}
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;
($priority{$a} and $priority{$b}) and return (rand > 0.5) ? 1 : -1;
($isnew{$a} and $isnew{$b}) and return (rand > 0.5) ? 1 : -1;

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;
}


I sure hope someone can help me!

Best Regards
Quote Reply
Re: [Coffee] Sorting Links (Yes I know...Not again!!) :-D In reply to
I recently installed the priority / random mod. The priority and new sorts work fine, but random is actually by link number.

The mod was as Alex posted.

Is there an update that I missed in reading the thread?

Thanks;

John