Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

CustomPagelistSortOrderFunctions

Summary: Custom functions for creating custom page sort orders with pagelists
Version: 2007-03-06
Prerequisites: pmwiki 2.2.0 beta
Status:
Maintainer:
Categories: Searching Pagelists

Description

Custom functions for creating custom page sort orders with pagelists.

Sorting pages by integer pagetextvar data:

Here is a custom PagelistSortOrder function for sorting pages by numerical PageTextVariable data.
The pagetextvariable $:Books is used as an example.

Add to local config (or recipe script):

# sorting pages by numerical integer pagetextvar data
function IntegerDataCompare($x, $y, $var) {
      # get integer value of the page text variable
      $xval = intval(PageTextVar($x, $var));
      $yval = intval(PageTextVar($y, $var));
      # compare integer values
      if($xval > $yval) $c = 1;
      elseif($xval < $yval) $c = -1;
      else $c = 0;
      return $c;
}

# defining order=books to sort integers numerical by pagetextvar $:Books
$PageListSortCmp['books'] = 'IntegerDataCompare($x, $y, "Books")';

Then use in your PageList directive with order=books (numerical sort smallest number first) or order=-books (largest number first)

For other integer page text variables just create another array item for $PageListSortCmp, as done above with "Books"

Sorting pages by integer numbers of pagename (Title)

Add to local config (or recipe script):

function IntegerNameCompare($x, $y) {
      # get integer value of the page name or title
      $xval = intval(PageVar($x, '$Title'));
      $yval = intval(PageVar($y, '$Title'));
      # compare integer values
      if($xval > $yval) $c = 1;
      elseif($xval < $yval) $c = -1;
      else $c = 0;
      return $c;
}
$PageListSortCmp['intname'] = 'IntegerNameCompare($x, $y)';

Then use in your PageList directive with order=intname (numerical sort smallest number first) or order=-intname (largest number first). To exclude other page names from the list and use only pagenames starting with a digit use in the pagelist directive: name=[0-9]*

Example: (:pagelist group={$Group} name=[0-9]* order=intname fmt=#title :)

Notes

Wouldn't it be easier to just use strnatcasecmp()? http://us3.php.net/manual/en/function.strnatcasecmp.php

$PageListSortCmp['natural'] = 'strnatcasecmp($x, $y)';

It can handle combinations of integers and text, so that Page11 < Page100, etc.

Ben Stallings March 15, 2007, at 01:08 PM

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

See Also

Contributors

Edit - History - Print - Recent Changes - Search
Page last modified on March 16, 2007, at 09:19 AM