Gossamer Forum
Home : Products : Links 2.0 : Customization :

Sort by date

Quote Reply
Sort by date
I've set the $db_sort_links equal to 3 which is the date field.

It is sorting by the date field, but numerically, not chronologically. How can I have Links sort by date, listing the newest entries on top?

Please help.

-Ryan

[This message has been edited by Ryan (edited January 25, 1999).]
Quote Reply
Re: Sort by date In reply to
Hi Ryan,

Look for sub build_sorthit in either db.pl or db_utils.pl depending on whether you are using 1.1 or 2.0.

Then change:

Code:
foreach $hit (sort {
($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;
($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)) {

to just:

Code:
foreach $hit (sort {
&date_to_unix ($sortby{$a}) <=> &date_to_unix ($sortby{$b})
} (keys %sortby)) {

Hopefully that should do the trick.

Cheers,

Alex
Quote Reply
Re: Sort by date In reply to
Actually, that gave me the exact opposite of what I need. It placed newest items at the bottom, including those marked new. What change will give me the opposite results?

Thanks,

-Ryan
Quote Reply
Re: Sort by date In reply to
hi, just change the code to:
Code:
foreach $hit (sort {
&date_to_unix ($sortby{$b}) <=> &date_to_unix ($sortby{$a})
} (keys %sortby)) {

It should do the job.

Regards,
Asterisk
Quote Reply
Re: Sort by date In reply to
Aha...

That worked. I almost tried that, but figured I could wait for someone who knew what they were doing.

Thanks,

-Ryan
Quote Reply
Re: Sort by date In reply to
Digging in the archives :-)

The above solutions works fine, but does anyone know how it then sorts for one day?

Is it possible to have it sort, first, by date, and then, for each day, by ID? This should give the most recent addition at the top, shouldn't it?

John
Quote Reply
Re: Sort by date In reply to
grrrr...

Any ideas what i'm doing wrong ?

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_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 {
&date_to_unix ($sortby{$b}) <=> &date_to_unix ($sortby{$a})
} (keys %sortby)) {

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

Code:
# Definition of your database file.
%db_def = (
ID => [0, 'numer', 5, 8, 1, '', ''],
Title => [1, 'alpha', 40, 75, 1, '', ''],
URL => [2, 'alpha', 40, 175, 1, 'http://', '^http|news|mailto|ftp'],
Width => [3, 'numer', 10, 10, 1, '', ''],
Height => [4, 'numer', 10, 10, 1, '', ''],
Filesize => [5, 'numer', 10, 10, 1, '', ''],
Dateline => [6, 'alpha', 40, 75, 1, '', ''],
Date => [7, 'date', 15, 15, 1, \&get_date, ''],
Category => [8, 'alpha', 0, 150, 1, '', ''],
AltCategories => [9, 'alpha', 0, 1000, 0, '', ''],
Description => [10, 'alpha', '40x3', 500, 0, '', ''],
'Contact Name' => [11, 'alpha', 40, 75, 0, '', ''],
'Contact Email' => [12, 'alpha', 40, 75, 0, '', '.+@.+\..+'],
Hits => [13, 'numer', 10, 10, 1, '0', '\d+'],
isNew => [14, 'alpha', 0, 5, 0, 'No', ''],
isPopular => [15, 'alpha', 0, 5, 0, 'No', ''],
isPick => [16, 'alpha', 0, 5, 0, 'No', ''],
Votes => [17, 'numer', 10, 10, 1, '0', '^\d+$'],
Rating => [18, 'numer', 10, 10, 1, '0', '^[\d\.]+$'],
ReceiveMail => [19, 'alpha', 10, 10, 1, 'Yes', 'No|Yes']
);

Code:
# Field number to sort links by:
$db_sort_links = 7;

Code:
2|#95 - Not On ABC, Please|http://216.55.29.184/images/cartoons/cart/rdameric.jpg|600|363|82|Road America '99|12-Nov-1999|Carlos_Carneiro/CART&#0124; &#0124;CART TV coverage cancelled to make way for poxt golf game!|Carlos|Suzzcar@aol.com|13|No|Yes|No|0|0|Yes
3|#91 - Colombian Donuts|http://216.55.29.184/images/cartoons/cart/juandnut.jpg|600|258|50|Portland '99|12-Nov-1999|Carlos_Carneiro/CART&#0124; &#0124;|Carlos Carneiro|Suzzcar@aol.com|3|No|No|No|0|0|Yes
4|#108 - Safe Bet|http://216.55.29.184/images/cartoons/f1/safebet.jpg|650|427|113|Japanese GP '99|13-Nov-1999|Carlos_Carneiro/F1&#0124; &#0124;|Carlos Carneiro|Suzzcar@aol.com|2|Yes|No|No|0|0|Yes
5|Brawnimoto-San|http://www.soft-toon.demon.co.uk/f199/gp9918.jpg|700|526|58|Malaysian GP '99|28-Oct-1999|Gurmit_Flora&#0124; &#0124;|Gurmit Flora|gurmit@soft-toon.demon.co.uk|4|No|No|No|0|0|Yes
6|Marketing Opportunities|http://www.soft-toon.demon.co.uk/f199/gp9901.jpg|650|434|82|Pre Season '99|14-Jan-1999|Gurmit_Flora&#0124; &#0124;|Gurmit Flora|gurmit@soft-toon.demon.co.uk|1|No|No|No|0|0|Yes
7|From Clothes To Cars|http://www.soft-toon.demon.co.uk/f199/gp9902.jpg|672|463|74|Pre Season '99|31-Jan-1999|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
8|Top secret developments at McLaren|http://www.soft-toon.demon.co.uk/f199/gp9903.jpg|450|624|70|Pre Season '99|18-Feb-1999|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
9|Australian GP|http://www.soft-toon.demon.co.uk/f199/bf0199.gif|629|536|49|Australian GP '99|12-Mar-1999|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|1|No|No|No|0|0|Yes
10|Flexible Wings|http://www.soft-toon.demon.co.uk/f199/gp9904.jpg|700|492|70|Australian GP '99|23-Mar-1999|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|1|No|No|No|0|0|Yes
11|Brazilian GP|http://www.soft-toon.demon.co.uk/f199/bf0299.gif|600|476|39|Brazilian GP '99|15-Apr-1999|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
12|Looking for San Marino|http://www.soft-toon.demon.co.uk/f199/gp9905.jpg|650|446|59|San Marino GP '99|29-Apr-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
13|San Marino GP|http://www.soft-toon.demon.co.uk/f199/bf0399.gif|400|544|30|San Marino GP '99|6-May-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
14|Don't Be Cruel|http://www.soft-toon.demon.co.uk/f199/gp9906.jpg|407|600|36|San Marino GP '99|11-May-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
15|Monaco GP|http://www.soft-toon.demon.co.uk/f199/bf0499.gif|400|547|53|Monaco GP '99|20-May-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
16|Wowie!|http://www.soft-toon.demon.co.uk/f199/gp9907.jpg|410|574|42|Monaco GP '99|26-May-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
17|Spanish GP|http://www.soft-toon.demon.co.uk/f199/bf0599.gif|650|484|42|Spanish GP '99|5-Jun-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
18|Montreal Airport GP|http://www.soft-toon.demon.co.uk/f199/gp9908.jpg|650|484|56|Canadian GP '99|10-Jun-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
19|Canadian GP|http://www.soft-toon.demon.co.uk/f199/bf0699.gif|700|484|39|Canadian GP '99|18-Jun-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
20|Aero mods for Prost|http://www.soft-toon.demon.co.uk/f199/gp9910.jpg|675|477|60|French GP '99|24-Jun-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
21|French GP|http://www.soft-toon.demon.co.uk/f199/bf0799.gif|400|562|31|French GP '99|1-Jul-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
22|Ultimate Jordan Babe|http://www.soft-toon.demon.co.uk/f199/gp9909.jpg|425|612|31|French GP '99|8-Jul-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes
23|British GP|http://www.soft-toon.demon.co.uk/f199/bf0899.gif|450|582|38|British GP '99|14-Jul-99|Gurmit_Flora&#0124; &#0124;&#0124; &#0124;|0|No|No|No|0|0|Yes

That little lot spews out: http://www.ten-tenths.com/toons/Gurmit_Flora/ which, as you will notice from the date which I added to aid bedugging, doesn't work out quite right! I got this mod to work on our Parc Ferme magazine, so I know it works - it's just me staring right past the obvious!

------------------
Craig Antill
10 Tenths Motorsport - www.Ten-Tenths.com/links/
Parc Ferme Magazine - www.Ten-Tenths.com/Parc_Ferme/
THE Directory - www.gardenbuildings.com/

Quote Reply
Re: Sort by date In reply to
Anyone ? Even a clue would be a help - i've been staring at this screen for far too long! Frown

------------------
Craig Antill
10 Tenths Motorsport - www.Ten-Tenths.com/links/
Parc Ferme Magazine - www.Ten-Tenths.com/Parc_Ferme/
THE Directory - www.gardenbuildings.com/

Quote Reply
Re: Sort by date In reply to
grrr... [getting restless] someone must know ?! I've been back through the code umpteen times...

HELP!

------------------
Craig Antill
10 Tenths Motorsport - www.Ten-Tenths.com/links/
Parc Ferme Magazine - www.Ten-Tenths.com/Parc_Ferme/
THE Directory - www.gardenbuildings.com/

Quote Reply
Re: Sort by date In reply to
Thanks for your help guys! Wink

I've managed to fix it now... when we entered the data, we used our own dates, but i'd entered 1999 and my assistant entered 99 - onbviously this was our cock up!

Anyway, all works now (thank god!)

------------------
Craig Antill
10 Tenths Motorsport - www.Ten-Tenths.com/links/
Parc Ferme Magazine - www.Ten-Tenths.com/Parc_Ferme/
THE Directory - www.gardenbuildings.com/

Quote Reply
Re: Sort by date In reply to
Thanks, Alex...exactly what I was looking for!

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------