note 107962 deleted from function.sort by danbrown

From: Date: Thu, 10 May 2012 18:02:43 +0000
Subject: note 107962 deleted from function.sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187709@lists.php.net to get a copy of this message
Note Submitter: Adrian at xdt dot co dot uk ---- To holdoffhunger 02-Jan-2012: Interesting approach you have there. How about this instead? <?php function longestfirst($str1, $str2) { return strlen($str2) - strlen($str1); } function shortestFirst($str1, $str2) { return strlen($str1) - strlen($str2); } $data = array("hello", "12", "php", "is", "awesome", "1", "1234", "123"); uasort($data, 'longestFirst'); echo '<pre>'; print_r($data); uasort($data, 'shortestFirst'); print_r($data); echo '</pre>'; /* Gives you: Array ( [4] => awesome [0] => hello [6] => 1234 [2] => php [7] => 123 [3] => is [1] => 12 [5] => 1 ) Array ( [5] => 1 [3] => is [1] => 12 [7] => 123 [2] => php [6] => 1234 [0] => hello [4] => awesome ) */ ?> The order is undefined where the strings have the same length. Hope that helps.

« previous php.notes (#187709) next »