Gossamer Forum
Home : Products : Links 2.0 : Customization :

Sorting wizard needed!

Quote Reply
Sorting wizard needed!
ARRGGHHH, my sorting routines are making me mad! I am sorting by date. It works fine in the what's new page. But on category pages, it works fine only for the first ten entries; when adding an elewenth entry it becomes a mess. Let me illustrate this with link ID-numbers:

With ten or less:
1
2
3
4
...
9
10

But when adding the 11th, it becomes:
1
11
2
3
4
...

That's simplified, actually. On one page, I have order: 18, 19, 20, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17.

What is wrong?

John
Quote Reply
Re: Sorting wizard needed! In reply to
John,

If you show us the sort routine, we'll probably be able to help. Based just on your description here, we'd only be guessing. Wink

Phoenix
Quote Reply
Re: Sorting wizard needed! In reply to
I've uploaded as txt files a few relevant files: http://www.shn.dk/links/

In db_utils.pl it says:
Quote:
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{$a}) <=> &date_to_unix ($sortby{$b})
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

The built results can be seen from http://www.shn.dk/nysajt.html

John
Quote Reply
Re: Sorting wizard needed! In reply to
If you give us an example we can help you. because if don't understand why you give us a id-number example if the problem is with the date.

...so 11-jan-1999 would be after 2-jan-1999?
Quote Reply
Re: Sorting wizard needed! In reply to
John,

Your sort routine looks to be correct for sorting by date.

Looking at a previous post you made in another thread, I think you are trying to sort first by date and then by ID number within a given date. Is that correct? I'm a bit confused.

Phoenix

Quote Reply
Re: Sorting wizard needed! In reply to
Sorry about this confusion. _I_ am indeed confused ...

I am working on several different systems using Links. In some of them, I want to control the sorting within categories in certain ways.

At first, I thought I could build 'smart' rountines, therefore the previous first-this-then-that ideas.

I am coming to the conclusion that what I need is to go with 'cleaner' installations, and dedicate them to one type of sorting.

So, simple questions:

A. How do I sort by ID-number and nothing else (not new, pop, etc.)? Ascending as well as decenting (for the learning)? One category's content is build in one session. and left as is. This is for example useful if publishing a magazine with a table of content.
In links.def I set:
# Field number to sort links by:
$db_sort_links = 0;
where $db_key = 0; has been added on the field number list above.
And then leave db_utils.pl in its original form (not my mod).
OK, I done that, and it works in one category, but not in another! Check http://www.shn.dk/ny/Artiklar/1999/3/ for the bad sorting (note 8 and 9 at them bottom!).

This is strange, isn't it?

Anyway (but why, oh why does it do that??), how do I get rid of the sort thing with new and pop?

B. I forgot that now ...

John
Quote Reply
Re: Sorting wizard needed! In reply to
John,

To sort by ID number only, without sorting by new and pop, change build_sorthit to the following:
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, $hit, $i, @sorted);

for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
}
foreach $hit (sort { return $sortby{$a} <=> $sortby{$b} } (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

As you see, I just deleted ever reference to new and pop to get rid of them.

The reason your numbers were sorting strangely was that you had them sorting alphabetically (that's my guess!). In the code above the line with $sortby{$a} <=> $sortby{$b} is what you need to make it sort numerically.

Now this will sort them in ascending order. 1 ...2...3...
To reverse them, simply switch the positions of $a and $b.

Oh, and of course make sure you have set the sort field to ID in links.def. Smile

Hope this helps.
Phoenix

[This message has been edited by phoenix (edited June 13, 1999).]
Quote Reply
Re: Sorting wizard needed! In reply to
Thanks Phoenix!

My sorting works now :-)

I must ask:

Is it possible to have this sorting done $a to $b in some categories and $b to $a in others?

John
Quote Reply
Re: Sorting wizard needed! In reply to
No problem, John. Your site looks very good, too, although I can not understand a word of it. Wink

It isn't possible to use different sort routines for different categories. Too bad, because I see how that could be useful.

Phoenix
Quote Reply
Re: Sorting wizard needed! In reply to
Thanks for your comments.

The problem below is raised in the installation forum - http://www.gossamer-threads.com/scripts/forum/resources/Forum2/HTML/000939.html

Now, something strange happened. Suddenly, it is as if the admin section cannot find the database :-(

- when clicking Add, there's no ID
- when searching for modifu for examoke, no result

I look in links.def, which I edited (sort by field, as mentioned above), but can't see what's wrong.

BTW, what does this do:
Quote:
# Use the built in key tracker.
$db_key_track = 1;
?

John

[This message has been edited by gotze (edited June 14, 1999).]

[This message has been edited by gotze (edited June 14, 1999).]

[This message has been edited by gotze (edited June 14, 1999).]