note 87884 modified in function.usort by danbrown

From: Date: Mon, 29 Dec 2008 02:07:38 +0000
Subject: note 87884 modified in function.usort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148628@lists.php.net to get a copy of this message
<?php /*--------------------------------------------------------- * my own home made easy object sort, which * i cannot find anywhere on the net * at all, tut. i can find only complicated ones, * which brings me a headache. * * You can enhance it to return all sorts of error * conditions if you wish, * i need nothing but a false return upon failure to sort, * so that is the only error i have here * * Example usage: * * INPUT - $objects_array // an array of objects for sorting * PARAMETERS - property // the property of the object to be sorted * OUTPUT - $engine // an engine object created by new, // with your arrays of objects sorted according to your // selected property //( the sorting is done upon sort object creation ) * * Code Examples * * $engine = new objSorter($objects_array); // sorts on FIRST property - default * $engine = new objSorter($objects_array,'property'); // sort on ANY specific property *--------------------------------------------------------*/ class objSorter { var $property; var $sorted; function ObjSorter($objects_array,$property=null) { $sample = $objects_array[0]; $vars = get_object_vars($sample); if (isset($property)) { if (isset($sample->$property)) // make sure requested property is correct for the object { $this->property = $property; usort($objects_array, array($this,'_compare')); } else { $this->sorted = false; return; } } else { list($property,$var) = each($sample); $this->property = $property; usort($objects_array, array($this,'_compare')); } $this->sorted = ($objects_array); } function _compare($apple, $orange) { $property = $this->property; if ($apple->$property == $orange->$property) return 0; return ($apple->$property < $orange->$property) ? -1 : 1; } } // end class ?> USAGE EXAMPLE DUMP ORIGINAL user Object ( [name] => Yunihe Jopoba [age] => 27 [richness] => € 899,970 ) user Object ( [name] => Janirevi Fakejorumoa [age] => 19 [richness] => € 219,078 ) user Object ( [name] => Korejowi Mezede [age] => 95 [richness] => € 3,272 ) user Object ( [name] => Nugoaivu Jongyi [age] => 84 [richness] => € 94,853 ) user Object ( [name] => Cepomase Buaesukoyua [age] => 32 [richness] => € 677,180 ) user Object ( [name] => Vejosehona Auva [age] => 20 [richness] => € 144,540 ) user Object ( [name] => Fewame Wojuvuzo [age] => 69 [richness] => € 198,231 ) user Object ( [name] => Risuku Zagedobu [age] => 48 [richness] => € 763,860 ) user Object ( [name] => Fazurada Aayu [age] => 16 [richness] => € 302,352 ) SORTED by age <?php $objects = new ObjSorter($objects_array,'age'); if ($objects->sorted) { $objects_array = $objects->sorted; foreach ($objects_array as $key=>$object) { print_r($object); echo "<br />"; } } ?> user Object ( [name] => Fazurada Aayu [age] => 16 [richness] => € 302,352 ) user Object ( [name] => Janirevi Fakejorumoa [age] => 19 [richness] => € 219,078 ) user Object ( [name] => Vejosehona Auva [age] => 20 [richness] => € 144,540 ) user Object ( [name] => Yunihe Jopoba [age] => 27 [richness] => € 899,970 ) user Object ( [name] => Cepomase Buaesukoyua [age] => 32 [richness] => € 677,180 ) user Object ( [name] => Risuku Zagedobu [age] => 48 [richness] => € 763,860 ) user Object ( [name] => Fewame Wojuvuzo [age] => 69 [richness] => € 198,231 ) user Object ( [name] => Nugoaivu Jongyi [age] => 84 [richness] => € 94,853 ) user Object ( [name] => Korejowi Mezede [age] => 95 [richness] => € 3,272 ) SORTED by richness <?php $objects = new ObjSorter($objects_array,'richness'); if ($objects->sorted) { $objects_array = $objects->sorted; foreach ($objects_array as $key=>$object) { print_r($object); echo "<br />"; } } ?> --was-- class objSorter { var $property; var $sorted; function ObjSorter($objects_array,$property=null) { $sample = $objects_array[0]; $vars = get_object_vars($sample); if (isset($property)) { if (isset($sample->$property)) // make sure requested property is correct for the object { $this->property = $property; usort($objects_array, array($this,'_compare')); } else { $this->sorted = false; return; } } else { list($property,$var) = each($sample); $this->property = $property; usort($objects_array, array($this,'_compare')); } $this->sorted = ($objects_array); } function _compare($apple, $orange) { $property = $this->property; if ($apple->$property == $orange->$property) return 0; return ($apple->$property < $orange->$property) ? -1 : 1; } } // end class http://php.net/manual/en/function.usort.php

« previous php.notes (#148628) next »