Location: PHPKode > scripts > Array Sorter > array_of_objects_example.php
<?php
/**
 * Example of sorting array of objects
 * 
 */

//test data
$ar = array();

$o = new stdClass();
$o->name = 'Terry';
$o->surname = 'Cook';
$o->addr = new stdClass();
$o->addr->street = 'ABBAY ROAD';
$o->addr->no = '10';
$o->salary = 25000;
$ar[0] = $o;

$o = new stdClass();
$o->name = 'Anna';
$o->surname = 'Smith';
$o->addr = new stdClass();
$o->addr->street = 'STREATHAM PLACE';
$o->addr->no = '1';
$o->salary = 15000;
$ar[1] = $o;

$o = new stdClass();
$o->name = 'John';
$o->surname = 'Doe';
$o->addr = new stdClass();
$o->addr->street = 'GAUNT STREET';
$o->addr->no = '101';
$o->salary = 30000;
$ar[2] = $o;


require('sorter.php');

//initialize sorter
$oSorter = new ArraySorter();
//set data to sort
$oSorter->setArray($ar);

echo "<pre>";
//sort by addr street ascending
print_r($oSorter->sort('addr.street', ArraySorter::DIRECTION_ASC));

echo '<hr />';
//sort by salary descending
print_r($oSorter->sort('salary', ArraySorter::DIRECTION_DESC));
Return current item: Array Sorter