Home : Products : Links 2.0 : Customization :

Products: Links 2.0: Customization: Re: [PerlFlunkie] How to sort by custom field?: Edit Log

Here is the list of edits for this post
Re: [PerlFlunkie] How to sort by custom field?
The picture (attachment) shows entries sorted by date - by category and in alphabetical order.

I hope that the following code sniplets will help you.




db_utils.pl : sub build_sorthit

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, $column, $type);

foreach $column (@db_cols) {
if ($db_sort_links == $db_def{$column}[0]) {
$type = $db_def{$column}[1];
last;
}
}
for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
($unsorted[$db_isnew + ($i * ($#db_cols+1))] eq "Ja") and ($isnew{$i} = 1);
($unsorted[$db_ratings + ($i * ($#db_cols+1))] eq "Ja") and ($iscool{$i} = 1);
}
if ($type eq "date") {
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]);
}
}
elsif ($type eq "numer") {
foreach $hit (sort {
$sortby{$b} <=> $sortby{$a};
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
}
else {
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;
}



nph-build.cgi - sub build_new_page

Code:
sub build_new_page {
# --------------------------------------------------------

my (%link_output, $category_clean, $long_date, $category, $date,
$number_links, $main_link_results, $main_total, %span_totals);
local ($total, $link_results, $title_linked);


if ($build_new_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}


$total = 0;
CATEGORY: foreach $category (sort keys %new_links) {
LINK: for ($i = 0; $i < ($#{$new_links{$category}}+1) / ($#db_cols + 1); $i++) {
$total++;
%tmp = &array_to_hash ($i, @{$new_links{$category}});
${$link_output{$tmp{'Anmeldedatum'}}}{$category} .= &site_html_link (%tmp) . "\n";
$span_totals{$tmp{'Anmeldedatum'}}++;
}
}


DATE: foreach $date (sort { &date_to_unix($b) <=> &date_to_unix($a) } keys %link_output) {
$long_date = &long_date ($date);
if ($build_new_span_pages) {
$link_results = "";
$total = $span_totals{$date};
$title_linked = &build_linked_title ("Neu/$long_date");
}
else {
$link_results .= "";
}
CATEGORY: foreach $category (sort keys %{$link_output{$date}}) {

$category_clean = &kategorie_title_mb ($category);
# Verändert für Listendarstellung ---------------------------------------------------------------------
$link_results .= qq|<tr style ="border : 1px solid #000000" bgcolor="#D4DFFB"><td style ="border : 1px solid #000000" colspan=6><font size=2>$long_date in: <A HREF="$build_root_url/$category/$build_index">$category_clean</A></font></td></tr>\n|;
# Ende Verändert für Listendarstellung ----------------------------------------------------------------
$link_results .= ${$link_output{$date}}{$category};
}

if ($build_new_span_pages) {
open (NEW, ">$build_new_path/$date$build_extension") or cgierr ("Kann die Datei bzw. Verzeichnis nicht öffnen: $build_new_path/$build_index. Grund: $!");
$use_html ?
print qq|\tNeue Links für <a href="$build_new_url/$date$build_extension" TARGET="_blank">$date</a>: $total\n| :
print qq|\tNeue Links für $date: $total\n|;
print NEW &site_html_new;
close NEW;
$main_link_results .= qq|<tr><td colspan=6><br><font size=2>Neuzug&auml;nge am <a href="$build_new_url/$date$build_extension"><b>$long_date</b></a> ($total)</font><hr noshade size=1></td></tr>|;
$main_total += $total;
}
else {
$link_results .= "";
}
}

if ($build_new_span_pages) {
$link_results = "$main_link_results";
$total = $main_total;
}
$title_linked = &build_linked_title ("Neu");


open (NEW, ">$build_new_path/$build_index") or cgierr ("Kann die Datei bzw. Verzeichnis nicht öffnen: $build_new_path/$build_index. Grund: $!");
print "\tNeue Einträge gesamt: $total\n";
print NEW &site_html_new(@new_links);
close NEW;
}

.


Andreas

Dr.Windows








Last edited by:

SevenSpirits: Sep 1, 2008, 11:21 AM

Edit Log: