note 111462 added to function.usort

From: Date: Thu, 21 Feb 2013 21:04:34 +0000
Subject: note 111462 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-193721@lists.php.net to get a copy of this message
Needed to sort an array of objects based on multiple properties. (ie: Group, then Last Name, then First Name) Doing this in multiple usorts fails because the second usort ends up shuffling the results of the previous usort because order is undefined when criteria are equal. Ended up writing a function that builds a comparison operator by nesting ternary operators for each field, then using that function in usort(). Hopefully it's of use to someone else. /* * Sorts an array of objects by a list of properties. * Each property can have the following options: * 'property' can be either a public variable or a method(). * 'order' can be ASC or DESC * 'comparer' can be string, number, or date * * Example: * sortObjectsByProperties($objects, array( * array('property'=>'getPrice()', order=>'ASC', 'comparer'=>'number'), * array('property'=>'getName()', order=>'DESC', 'comparer'=>'string'), * array('property'=>'created_date', order=>'ASC', 'comparer'=>'date'), * array('property'=>'id', order=>'ASC', 'comparer'=>'number'), * )); * * @author Tyler Chamberlain */ function sortObjectsByProperties(&$objects, $fields) { $comparerFunctionString = ""; foreach ($fields as $field) { if ($field['comparer'] == 'number') { $comparerFunction = "(\$a->{$field['property']} == \$b->{$field['property']} ? 0 : (\$a->{$field['property']} > \$b->{$field['property']} ? 1 : -1))"; } elseif ($field['comparer'] == 'date') { $comparerFunction = "(strtotime(\$a->{$field['property']}) > strtotime(\$b->{$field['property']}) ? 1 : -1)"; } else { $comparerFunction = "strcasecmp(\$a->{$field['property']},\$b->{$field['property']})"; } if ($field['order'] === 'DESC') { $comparerFunction = "-".$comparerFunction; } $comparerFunctionString .= "$comparerFunction != 0 ? $comparerFunction : ("; } $comparer = "return ($comparerFunctionString 0 )".str_repeat(")", sizeof($fields)).";"; usort($objects, create_function('$a,$b', $comparer)); } ---- Server IP: 69.147.83.199 Probable Submitter: 173.174.89.231 ---- Manual Page -- http://php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/111462 Del: integrated -- https://master.php.net/note/delete/111462/integrated Del: useless -- https://master.php.net/note/delete/111462/useless Del: bad code -- https://master.php.net/note/delete/111462/bad+code Del: spam -- https://master.php.net/note/delete/111462/spam Del: non-english -- https://master.php.net/note/delete/111462/non-english Del: in docs -- https://master.php.net/note/delete/111462/in+docs Del: other reasons-- https://master.php.net/note/delete/111462 Reject -- https://master.php.net/note/reject/111462 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#193721) next »