Re: Sort by string length...
| From: | Tom Rogers | Date: | Tue, 21 Dec 2004 23:46:24 +0000 |
| Subject: | Re: Sort by string length... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-204899@lists.php.net to get a copy of this message | ||
Hi,
Wednesday, December 22, 2004, 7:18:52 AM, you wrote:
RPJ> Any idea how to sort an array by string length?
RPJ> Russ Jones
With a user defined sorting function something like this
<?php
function cmp($a, $b){
$x = strlen($a);
$y = strlen($b);
if ($x == $y) {
return 0;
}
return ($x < $y) ? -1 : 1;
}
$array = array('Longest string','short','longer');
usort($array,'cmp');
?>
--
regards,
Tom