* Copyright (c) 2011 Przemek Berezowski (firstname.lastname@example.org)
* All rights reserved.
* @category Library
* @package ArraySorter
* @copyright Copyright (c) 2011 Przemek Berezowski (email@example.com)
* @version 0.9
* @license New BSD License
ArraySorter class can be used to sort multidimensional arrays.
It takes multidimensional array or an array of objects and sorts it
by the given value.
The sorting entry name may include one more entry names, one per each dimension.
The sorting order may be configured also to be ascending or descending.
2. Preparing ArraySorter to do a job
First of all create the instance of ArraySorter.
Secondly set the array to sort using ArraySorter method setArray($array2Sort);
In the class there is public method sort($field, $direction)
which can be used to sort given array.
- Sorting by given field
$field param is a string desribing field position in array.
Each dimension of array is separated by a dot "."
Suppose we have such an array
['name'] = 'something';
['vandor']['name'] = 'company';
['vandor']['address']['street'] = 'abbay road';
['name'] = 'something else';
['vendor']['name'] = 'company';
['vendor']['address']['street'] = 'abbay road';
- to sort by name set param $field to 'name'
- to sort by vendor name set param $field to 'vendor.name'
- to sort by vendor street set param $field to 'vendor.address.street'
The same naming convention are used to sort array of objects or mixed structures
where araay can contains objecs which contains array fields.
- Define sort order
To define sort order, there are two constants in ArraySorter
ArraySorter::DIRECTION_ASC - defining ascending sorting direction
ArraySorter::DIRECTION_DESC - defining descending sorting direction
These contants can be used as a second parametr for sort method.
4. Any bugs or question please report to firstname.lastname@example.org