Gossamer Forum
Home : Products : Links 2.0 : Customization :

Another Sort Question - simple I think

Quote Reply
Another Sort Question - simple I think
Before I ask my question, I would like to thank everyone for all the posts they have made. I have installed and I'm in the process of customizing ... and the best part is that for every question I had I found the answer here on the forum .. including adding fields.

The answer to this question is probably here, but I got lost in the "site specific" questions regarding sort.

In links.def there is a section:

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

Can I just change this to reflect the fields I have added and wish to be sorted by .. ie:

# Field number to sort links by:
$db_sort_links = 16,14;

can it be this simple ?

Thanks
Jeff

Quote Reply
Re: Another Sort Question - simple I think In reply to
Jeff,

yes and no... All you have to do is change that number if you want to sort alphabetically. If you want to sort numerically you have to make one other tiny change.

And you can not sort by more than one field. It can be a problem in some cases. Frown But you can only select one field to sort by.

Alex helped me come up with a work-around method to sort by more than one field, but it will only work in certain situations. If you can tell me what the fields that you want to sort by contain, I *might* be able to help you with the work-around. If you desperately need this feature like I did. Wink

Phoenix
Quote Reply
Re: Another Sort Question - simple I think In reply to
Hi Phoenix,
Yes, I would be happy to and I should posted fields types the first time around.

They are both 'alpha' and they are also both "added" fields by me.

And thanks Phoenix .. it was your posting in FAQ that allowed me to add the fields.

Thanks again,
Jeff
Quote Reply
Re: Another Sort Question - simple I think In reply to
Jeff,

What I needed to do was sort by Last Name and then by First Name, because if I just sorted by Last Name, John Smith would get listed before Ann Smith if John added his link first.

My idea was to have Links combine those fields into one long string of text for the purpose of sorting only. So it would always sort SmithAnnJ before SmithJohnD. This would work in any situation where the fields are all alpha.

Take a look at the following thread and see if it helps. If it's not detailed enough and you need help, I'd be happy to explain the process.

www.gossamer-threads.com/scripts/forum/resources/Forum3/HTML/001199.html

Hope this is what you're looking for.

Phoenix

[This message has been edited by phoenix (edited June 13, 1999).]
Quote Reply
Re: Another Sort Question - simple I think In reply to
Hi Phoenix,

Thanks for the help. I think I understand most of it, excpet the last part which I've commented on below.

# replace the following in build_sort_hit in db_utils.pl ***

$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];

# with

my $offset = $i * ($#db_cols+1);
$sortby{$i} = join ("", @unsorted[$db_field1+ $offset, $db_field2 + $offset, $db_middlename + $field3]);

# replace fields1-3 with my fields

# from here forward I'm confused and not sure what to put into the line of code in links.def

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

#? Do I create a new field in links.def and then use that number and if so what should I name it?

Thanks again,
Jeff

Quote Reply
Re: Another Sort Question - simple I think In reply to
I would like to sort on a added field but it I want it to be a date field. Are you able to do this also and how would i go about it ?

Rad
Quote Reply
Re: Another Sort Question - simple I think In reply to
Hi Radman,
I'm not sure how to do the date, but I think this link will help you.

http://www.gossamer-threads.com/scripts/forum/resources/Forum3/HTML/000554.html

If that doesn't work for you try the search feature, I recall reading serval pages on sorting on the date, while I was trying to figure out how to sort on two fields.

I found it by using search on "date sorting"

Good luck
Jeff
Quote Reply
Re: Another Sort Question - simple I think In reply to
Radman,
I just posted the code to sort by date yesterday at http://www.gossamer-threads.com/...um3/HTML/001844.html

Jeff,

Looks like you've got it, except that you left in $db_lastname rather than putting in "offset". Smile You also need to make sure you've defined those fields in links.def in the section where it has all the $db_whatever=1 and so on. It doesn't really matter what you name them there, just so it's something you'll remember.

It doesn't matter what you put it the sortby variable in links.def, or whether you have nothing listed there at all, because using this method, you will not use that variable anymore.

Hope this does what you want it to do. It was the perfect solution for my problem.

Phoenix
Quote Reply
Re: Another Sort Question - simple I think In reply to
I think I got it.

Don't worry anymore about creating a new field to store the rsults of appending Field1 + Field2 + Field3 or adding something to:
# Field number to sort links by:
$db_sort_links = 1;

It uses a temp Mem Var and does everything on the fly ... right ?

Oh and the code should actually be:

my $offset = $i * ($#db_cols+1);
$sortby{$i} = join ("", @unsorted[$db_field1+ $offset, $db_field2 + $offset, $field3 + $offset]);

.. there was two typos in there I think.


Thanks
JEff
Quote Reply
Re: Another Sort Question - simple I think In reply to
Jeff,

Yep, you got it. Isn't that nifty? I was so pleased with that, especially after spending days trying to figure out how to sort by more than one field and having searched the archives to find many posts that said it couldn't be done. Wink

Phoenix
Quote Reply
Re: Another Sort Question - simple I think In reply to
Eliot,

Your links are sorting properly. What Links does, by default no matter what field you choose to sort by, is sort New links first, Cool links second, and then everything else in alphabetic order. That's what's happening on your page.

If you want it to only sort alphabetically, you will need to change a few things in the sort routine in db_utils.pl. In that file, find sub build_sorthit and replace it wilth 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 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;
}

Hope this helps.
Phoenix

[This message has been edited by phoenix (edited July 05, 1999).]
Quote Reply
Re: Another Sort Question - simple I think In reply to
I apologize for posting this question twice (but I found a similar Topic in the Links Discussion Forum, so I posted it over there,
yet since I see the Topic getting buried by other Topics and it does seem more appropriate to post it in this forum, I am).

Anyway, I found a glitch in the sorting function of LINKS2. Most of my pages are sorted alphabetically by Title (field #1).
However, on a few pages, the sorting is whacked.

For example, check out:

www.anthrotech.com/resources/General

You will notice that the links are all out of order.

Anyone know how to fix this???

BTW: I do have the sorting variable set-up properly:

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

(I have changed directories three times and have rebuilt the directory MANY times.)

TIA

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited July 05, 1999).]
Quote Reply
Re: Another Sort Question - simple I think In reply to
Thanks, Phoenix. Works like a charm!
(It would be nice to provide this optional not to the user, but end user, so that they can customize the look of the directory).

But for now, it is a more logical listing
(by alphabetical).

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Another Sort Question - simple I think In reply to
Hi, Thanks to Phoenix and Alex for working on multiple fields sorting.

I would like to sort by:
New links,
field1,
field2.

field1 and field2 are alpha and I want to join them.

Right now I have
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 {
($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)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

Would you please tell me what I need to change in the above code.

Thanks a lot

Pat
happyrobot.com
Quote Reply
Re: Another Sort Question - simple I think In reply to
HI,

I did tried the suggestions in this thread. I now know what would be the best way to sort my links. I created a field name "Sort".
This field is set by default with the value "n". I'm planning to make that field an "a" for the sites that I think are good.

I tried the following code. It's working great! But now, I would like to take off the sorting by isNew and isPop. Would you know what to change??? Thanks for helping.

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++) {
my $offset = $i * ($#db_cols+1);
$sortby{$i} = join ("", @unsorted[$Sort+ $offset, $Title + $offset]);
($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 {
($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)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
return @sorted;
}

What I'm trying to do now is to sort first by "Sort" and second by "Title". What should I change???

Thank you very much for helping.

Pat

[This message has been edited by lancier (edited September 30, 1999).]
Quote Reply
Re: Another Sort Question - simple I think In reply to
Ok, I did it myself.
To sort first with field "Sort" and then with field "Title". That way you can control the sorting order of each link. Ideal for premium sponsors or links you want to keep at the end.

First:
add the field "Sort" in links.def
[QUODE]
Sort => [69, 'alpha', 2, 2, 1, 'n', ''],[/QUODE]

and the add $db_password = 70 in the important fields...(just under all links)

The change sub build_sorthit in db_utils.pl for:
Quote:
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_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;
}

I hope it could help some of you. Thanks to ALex and Phoenix. All I did is merge their code together....


Pat
Quote Reply
Re: Another Sort Question - simple I think In reply to
I don't need to merge fields or anything that complex, but say I installed the editor's pick mod and wanted to sort by 'new' then by 'pick' instead of 'cool'... is it really as simple as changing the references to 'iscool' in build_sorthit to 'ispick' or whatever i've called the field?
Quote Reply
Re: Another Sort Question - simple I think In reply to
pretty much... Smile

just make sure you put the right scalar in there..

you change EVERY occurance in there to ispick

EVERY.. even the %iscool and then the $iscool{$i} thing..

also.. you change this one.. $db_ispop

to

$db_ispick

jerry