Gossamer Forum
Home : Products : Links 2.0 : Customization :

Help with links Sorting by Numeric field

Quote Reply
Help with links Sorting by Numeric field
I want to sort by 'Sort', 'isNew', 'Rating', 'Votes', 'Title'

after searching this forum for 3 hours:
added to links.def:
...
Sort => [14, 'alpha', 2, 2, 1, 'n', '']
...
$db_sort = 14;
...
$db_sort_links = 1; (left default)

added new field to all records.

changed in db_util.pl:
*****************************
sub build_sorthit {
# --------------------------------------------------------
# This function sorts a list of links. It has been modified to sort alphabetically field "Sort" and then "____". By modifying
# the sort function below, you can sort the links however you like. Just change sort to "a" for the links you want
# first (ie. premium listing) and to "n" for normal links
#
my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, $hit, $i, @sorted);
for ($i = 0; $i < $num; $i++) {
my $offset = $i * ($#db_cols+1);
$sortby{$i} = join ("", @unsorted[$db_sort + $offset, $db_isnew + $offset, $db_rating + $offset, $db_votes + $offset, $db_title + $offset]);
}
foreach $hit (sort {
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;
}

****************************
It puts records with 'Sort'='a' before 'Sort'='n', as I need it.
But the rest of sorting is not working. Ratings sorts backwards and, i think, i have to tell the script to sort 'Rating' and 'Votes' as numeric not text fields.

What should I do? Please help.

Anton Pachkine [ anton@27stars.com ]
www.OnlineBusinessDirectory.org
- Built by People for People

================================================
PS: Have a great service, article, eBook or eZine
for online entrepreneurs?
Register it for hundreds of people to find!
http://www.OnlineBusinessDirectory.org
================================================



Quote Reply
Re: Sort by Sponsor, Rating, Votes solution In reply to
<pre>
Ok, I did it myself. Thanks anyway (for opportunity to think :)

Actually it's very simple. If somebody wants to sort by [Sponsor, Rating, Votes] (Sample: www.OnlineBusinessDirectory.org)

Here is how to do it:
1) links.def
1.1) add field for sorting criteria (0 - normal link, 9 - sponsor at the top. '0' - default)

Sort => [14, 'numer', 5, 8, 1, '0', '']

1.2) Add field number at the end of important fields:

$db_sort = 14;

2) db_utils.pl
Change sub build_sorthit sub a new one:

================================
sub build_sorthit {
# --------------------------------------------------------
# This function sorts a list of links. It has been modified to sort alphabetically field "Sort" and then "Title". By modifying
# the sort function below, you can sort the links however you like. Just change sort to "a" for the links you want
# first (ie. premium listing) and to "n" for normal links
#
my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, $hit, $i, @sorted);
for ($i = 0; $i < $num; $i++) {
my $offset = $i * ($#db_cols+1);

$sortby{$i} = join ("", @unsorted[$db_sort + $offset, $db_rating + $offset, $db_votes + $offset]);
}
foreach $hit (sort {

return $sortby{$b} <=> $sortby{$a}

} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}
===========================================

3) Add 'Sort' field to every record in your database.
The best way for me is using Telnet:
Originally posted by Frank:
==========================
If you have access to telnet for your website, I find awk (or gawk, nawk whatever is installed) a lot easier to use for this type of coding. Here is a sample awk program that would do the same thing:

code in text file convert.awk:
--------------------------------------------------------------------------------

BEGIN{FS="|"
RS="\n"
OFS="|"
ORS="\n"}
{print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14 , "0"}

--------------------------------------------------------------------------------

FS = field seperator
RS = record seperator
OFS = output field seperator
ORS = output record seperator
$1 - $n = 1st - nth input field

To run the script (assume the script is in a file called convert.awk) use the following:

code:
--------------------------------------------------------------------------------

awk -f convert.awk < links.db > new_links.db

--------------------------------------------------------------------------------

You'll need to supply the full paths to convert.awk, links.db and new_links.db

============================

Check new_links.db and rename it to links.db

Change Sort field for Sponsors' links to 9 (6,7,8 > 0)
Rebuild All.

It works for me,
People-programmers! Is it correct?

By the way, our visitors asked me to create a new section:
'Contacts/Programmers' and list people who can build
and(or) install CGI scripts for various projects.
If you are interested, please register at
www.OnlineBusinessDirectory.org
(it's free and will bring you new clients)

Anton Pachkine [ anton@27stars.com ]
www.OnlineBusinessDirectory.org
- Built by People for People
</pre>