Detailed Description

NodeComparator is used to compare nodes by given criterias.

The following example shows the usage:

$nodeList = [...]; // array of Node instances
// simple sort by creator attribute
$comparator = new NodeComparator('creator');
usort($nodeList, [$comparator, 'compare']);
// sort by creator attribute with direction
$comparator = new NodeComparator('creator DESC');
usort($nodeList, [$comparator, 'compare']);
// sort by multiple attributes with direction
$comparator = new NodeComparator(['creator DESC', 'created ASC']);
usort($nodeList, [$comparator, 'compare']);
// more complex example with different attributes
$sortCriteria = [
$comparator = new NodeComparator($sortCriteria);
usort($nodeList, [$comparator, 'compare']);
ingo herwig

Definition at line 47 of file NodeComparator.php.

Public Member Functions

 __construct ($sortCriteria)
 compare (Node $a, Node $b)

Public Attributes

const SORTTYPE_ASC = -1
const SORTTYPE_DESC = -2
const ATTRIB_OID = -3
const ATTRIB_TYPE = -4

Constructor & Destructor Documentation

__construct (   $sortCriteria)


$sortCriteriaAn assoziative array of criteria - SORTTYPE constant pairs OR a single criteria string. possible criteria: NodeComparator::OID, NodeComparator::TYPE or any value/property name with optionally ASC or DESC appended (e.g. [NodeComparator::OID => NodeComparator::SORTTYPE_ASC, 'name' => NodeComparator::SORTTYPE_DESC] OR 'name')
If criteria is only a string we will sort by this criteria with NodeComparator::SORTTYPE_ASC

Definition at line 63 of file NodeComparator.php.

Member Function Documentation

compare ( Node  $a,
Node  $b 

Compare function for sorting Nodes by the list of criterias.

$aFirst Node instance
$bFirst Node instance
-1, 0 or 1 whether a is less, equal or greater than b in respect of the criteria

Definition at line 85 of file NodeComparator.php.

Member Data Documentation

const SORTTYPE_ASC = -1

Definition at line 49 of file NodeComparator.php.

const SORTTYPE_DESC = -2

Definition at line 50 of file NodeComparator.php.

const ATTRIB_OID = -3

Definition at line 51 of file NodeComparator.php.

const ATTRIB_TYPE = -4

Definition at line 52 of file NodeComparator.php.