note 72148 added to function.uasort

From: Date: Thu, 04 Jan 2007 17:54:37 +0000
Subject: note 72148 added to function.uasort
Groups: php.notes 
Request: Send a blank email to php-notes+get-121293@lists.php.net to get a copy of this message
Using uasort to alphabetically sort nested objects: In this example, a "collection" object contains an array of "dataItem" objects which consist of a string name, a string attribute x and an arbitrary integer y. This code allows you to sort the dataset by any of the dataItem attributes, or the order in which they were originally added to the set. I'm using PHP 4.23 at work at the moment for legacy reasons, so I wrote the example following that object model. <?php class dataItem { var $name; var $x; //Constructor function dataItem($name,$x,$y) { $this->name = $name; $this->x = $x; $this->y = $y; } } class collection { var $dataSet = array(); //Creates a new data item and adds it to our array function add($name,$x,$y) { $this->dataSet[] = new dataItem($name,$x,$y); } //The wrapper sort function function sortDataSet($s) { //Sort by the given parameter switch($s) { case "name": //Note use of array to reference member method of this object in callback uasort($this->dataSet,array($this,"cmpName")); break; case "x": uasort($this->dataSet,array($this,"cmpX")); break; case "y": uasort($this->dataSet,array($this,"cmpY")); break; case "added": default: //Re-sort array by original keys ksort($this->dataSet); } } //Callback function for sorting by name //$a and $b are dataItem objects function cmpName($a,$b) { //Use sort() for simple alphabetical comparison //Convert to lowercase to ensure consistent behaviour $sortable = array(strtolower($a->name),strtolower($b->name)); $sorted = $sortable; sort($sorted); //If the names have switched position, return -1. Otherwise, return 1. return ($sorted[0] == $sortable[0]) ? -1 : 1; } //Callback function for sorting by x //$a and $b are dataItem objects function cmpX($a,$b) { //Use sort() for simple alphabetical comparison //Convert to lowercase to ensure consistent behaviour $sortable = array(strtolower($a->x),strtolower($b->x)); $sorted = $sortable; sort($sorted); //If the names have switched position, return -1. Otherwise, return 1. return ($sorted[0] == $sortable[0]) ? -1 : 1; } //Callback function for sorting by y //$a and $b are dataItem objects function cmpY($a,$b) { //If $a's y attribute >= $b's y attribute, return 1. Otherwise, return -1. return ($a->y >= $b->y) ? 1 : -1; } } //Create a collection object $myCollection = new collection(); //Add a few "records" $myCollection->add("pancake","egg",7); $myCollection->add("France","Paris",2); $myCollection->add("Naked Gun","Leslie Nielsen",1); $myCollection->add("OSX","huge icons",33); $myCollection->add("telephone","keypad",-3); //Test the output //Sort by name: $myCollection->sortDataSet("name"); print_r($myCollection->dataSet); //Sort by x $myCollection->sortDataSet("x"); print_r($myCollection->dataSet); //Sort by y $myCollection->sortDataSet("y"); print_r($myCollection->dataSet); //Sort by order added $myCollection->sortDataSet("added"); print_r($myCollection->dataSet); ?> Will give this output (Anotated and re-formatted for ease of reading): Sorted by name: France Paris 2 Naked Gun Leslie Nielsen 1 OSX huge icons 33 pancake egg 7 telephone keypad -3 Sorted by x: pancake egg 7 OSX huge icons 33 telephone keypad -3 Naked Gun Leslie Nielsen 1 France Paris 2 Sorted by y: telephone keypad -3 Naked Gun Leslie Nielsen 1 France Paris 2 pancake egg 7 OSX huge icons 33 Sorted as added: pancake egg 7 France Paris 2 Naked Gun Leslie Nielsen 1 OSX huge icons 33 telephone keypad -3 This is obviously a trivial case, but it can be very useful for larger data structures. ---- Server IP: 83.138.144.80 Probable Submitter: 193.130.182.41 ---- Manual Page -- http://www.php.net/manual/en/function.uasort.php Edit -- https://master.php.net/note/edit/72148 Del: integrated -- https://master.php.net/note/delete/72148/integrated Del: useless -- https://master.php.net/note/delete/72148/useless Del: bad code -- https://master.php.net/note/delete/72148/bad+code Del: spam -- https://master.php.net/note/delete/72148/spam Del: non-english -- https://master.php.net/note/delete/72148/non-english Del: in docs -- https://master.php.net/note/delete/72148/in+docs Del: other reasons-- https://master.php.net/note/delete/72148 Reject -- https://master.php.net/note/reject/72148 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121293) next »