note 71120 added to function.usort

From: Date: Mon, 13 Nov 2006 11:43:30 +0000
Subject: note 71120 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-119654@lists.php.net to get a copy of this message
If you want to sort an array of strings by the length of its values, you can write a better cmp-function than manually comparing string lengths with if-statements. Unfortunately, I see this bad technique in other people's code. I prefer using this: <?php function length_cmp( $a, $b ) { return strlen($a)-strlen($b) ; } ?> This function sorts the array in ascending order. If you want to sort descending, simply swap $a and $b either in the parameters or in the subtraction. -Tim ---- Server IP: 217.160.72.57 Probable Submitter: 84.132.185.215 ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/71120 Del: integrated -- https://master.php.net/note/delete/71120/integrated Del: useless -- https://master.php.net/note/delete/71120/useless Del: bad code -- https://master.php.net/note/delete/71120/bad+code Del: spam -- https://master.php.net/note/delete/71120/spam Del: non-english -- https://master.php.net/note/delete/71120/non-english Del: in docs -- https://master.php.net/note/delete/71120/in+docs Del: other reasons-- https://master.php.net/note/delete/71120 Reject -- https://master.php.net/note/reject/71120 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119654) next »