spunQ_CallbackSorter Class Reference

Collection of sorting functions using callbacks. More...

List of all members.

Static Public Member Functions

static sortByKeys (&$items, $keys)
 Sorts an array based on a defined key order.
static stableSort (array &$items, $comparator, $keepKeys=false)
 Use a stable sorting algorithm to sort on an array.
static stableSortMap (array &$keys, array &$values, $comparator)
 Use a stable sorting algorithm to sort on a ma map.

Static Private Member Functions

static mergeSort (&$items, $comparator, $offset, $length)
 An implementation of merge sort.
static merge (&$items, $comparator, $offset1, $length1, $offset2, $length2)
 A merge function for the merge sort implementation.
static swap (&$items, $offset1, $offset2)
 Swaps two elements of an array.


Detailed Description

Collection of sorting functions using callbacks.

Started with the need for stable sorting algorithm, which PHP doesn't provide.

Definition at line 8 of file CallbackSorter.class.php.


Member Function Documentation

static spunQ_CallbackSorter::merge ( &$  items,
comparator,
offset1,
length1,
offset2,
length2 
) [static, private]

A merge function for the merge sort implementation.

Merges two sorted chunks of the array.

See also:
http://en.wikipedia.org/wiki/Merge_sort
Parameters:
$items The items to sort.
$comparator A comparing function as accepted by spunQ_Callback::call(). See stableSort() for an in-depth explanation.
offset1 The start offset of the first chunk to merge.
length1 Length of the first chunk.
offset2 The start offset of the second chunk to merge.
length2 Length of the second chunk.
Returns:
void The operation is performed in-place.

Definition at line 131 of file CallbackSorter.class.php.

static spunQ_CallbackSorter::mergeSort ( &$  items,
comparator,
offset,
length 
) [static, private]

An implementation of merge sort.

All operations are performed in-place.

See also:
http://en.wikipedia.org/wiki/Merge_sort
Parameters:
$items The items to sort.
$comparator A comparing function as accepted by spunQ_Callback::call(). See stableSort() for an in-depth explanation.
$offset The array index to start sorting from.
$length The array index to sort until.
Returns:
void The operation is performed in-place.

Definition at line 97 of file CallbackSorter.class.php.

static spunQ_CallbackSorter::sortByKeys ( &$  items,
keys 
) [static]

Sorts an array based on a defined key order.

E.g. useful for sorting the return value of getByIds(). Example:

   $objects = db()->getByIds($ids, $type);
   spunQ_CallbackSorter::sortByKeys($objects, $ids)
Returns:
void

Definition at line 20 of file CallbackSorter.class.php.

static spunQ_CallbackSorter::stableSort ( array &$  items,
comparator,
keepKeys = false 
) [static]

Use a stable sorting algorithm to sort on an array.

Stable means that objects with the same want change order.

Parameters:
$items Array of items to sort. Note that this is a reference and the operation will be performed in-place.
$comparator A comparing function as accepted by spunQ_Callback::call(). The function must return a negative value, if the first parameter is smaller than the second value, 0 if they are equal and a positive value otherwise. This format is the same as for the builtin function usort(). See http://php.net/usort.
$keepKeys Whether key order should be maintained. Setting this to true will slow down function a bit.
Returns:
void The operation is performed in-place.

Definition at line 45 of file CallbackSorter.class.php.

static spunQ_CallbackSorter::stableSortMap ( array &$  keys,
array &$  values,
comparator 
) [static]

Use a stable sorting algorithm to sort on a ma map.

Stable means that objects with the same want change order. Note that maps actually do not have an order, that's why the keys and values are passed separately. Note that each key/value pair will be matched by common array keys as described in spunQ_Map::getKeys().

See also:
spunQ_Map::getKeys()
Parameters:
$keys The keys of the map. The keys are adjusted to match the indexes of the values array. The values are the ones that are actually sorted.
$values The values to sort. Note that this parameter is a reference, the sorting is done in-place.
$comparator A comparing function as accepted by spunQ_Callback::call(). The function must return a negative value, if the first parameter is smaller than the second value, 0 if they are equal and a positive value otherwise. This format is the same as for the builtin function usort(). See http://php.net/usort.
Returns:
void The operation is performed in-place.

Definition at line 76 of file CallbackSorter.class.php.

static spunQ_CallbackSorter::swap ( &$  items,
offset1,
offset2 
) [static, private]

Swaps two elements of an array.

Parameters:
$items the array to swap items in.
$offset1 index of the first item.
$offset2 index of the second item.
Returns:
void The operation is performed in-place.

Definition at line 156 of file CallbackSorter.class.php.


The documentation for this class was generated from the following file:

Generated on Fri Jul 1 11:12:39 2011 for spunQ3 by  doxygen 1.5.9