[pmwiki-users] PageList sort order (numerical values)

Petko Yotov 5ko at 5ko.fr
Sat Apr 4 09:48:46 CDT 2015


On 2015-04-04 11:05, ABClf wrote:
> I am willing to get pagelist results sorted by popularity : i.e. count
> backlinks (targets). That way, most linked pages will be shown first.
> 
> For counting, targets, in config, I have :
> 
> # NbSource
> $FmtPV['$NbSource'] = 'NbSource($page["targets"])';
> function NbSource($targets) {
>   $cnt = substr_count($targets, "Source.");
>   return $cnt>0? "$cnt" : '';
> }

The entry $page["targets"] contains "frontlinks" not backlinks, these 
are internal pages to where the page from the pagelist links.

> (:pagelist group=Bob link={*$FullName} fmt=#description2 
> order=$NbSource:)
> 
> Problem now, is how to make value 2 before value 11, and so on (natural
> order).

I assume you want to make pages with more (11) links to appear before 
pages with less (2) links?

To order correctly with the PmWiki pagelist sort, you can convert the 
numbers to padded strings:

  function NbSource($targets) {
    $cnt = substr_count($targets, "Source.");
    return sprintf('%08d', $cnt); # see php.net/sprintf
  # or: return sprintf('%08d', 9999999-$cnt); # in decremental order
  }

Alternatively, it is possible to write your own pagelist sort function 
which may perform faster. On pmwiki.org the string sorting takes most of 
the pagelist processing time. Something like this:

  function NbSource($targets) {
    return substr_count($targets, "Source.");
  }

  # permute $x and $y to have it sort the other way
  $PageListSortCmp['nbsource'] = 
'NbSource($x["targets"])-NbSource($y["targets"])';

Then in the wiki, use (:pagelist ... order=nbsource:) or order=-nbsource

Petko




More information about the pmwiki-users mailing list