note 107372 added to function.usort

From: Date: Sat, 04 Feb 2012 15:58:10 +0000
Subject: note 107372 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-185851@lists.php.net to get a copy of this message
I needed a sort method that would sort strings but take note of any numbers and would compare them as number. I also want to ignore any non alphanumerical characters. Eg. Slot 1 Example Slot 10 Example Slot 2 Example Should infact be Slot 1 Example Slot 2 Example Slot 10 Example <?php function sort_with_numbers($a , $b) { $a = explode(' ',$a); $b = explode(' ',$b); $size = min(count($a), count($b)); for($index =0; $index < $size; ++$index) { $a1 = ereg_replace("[^A-Za-z0-9]", "",$a[$index]); $b1 = ereg_replace("[^A-Za-z0-9]", "",$b[$index]); $equal = 0; if (is_numeric($a1) && is_numeric($b1)) { $equal = $a1 - $b1; } else { $equal = strcasecmp($a1,$b1); } if ($equal < 0) { return -1; } if ($equal > 0) { return 1; } } return count($a) - count($b); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 80.1.185.48 ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/107372 Del: integrated -- https://master.php.net/note/delete/107372/integrated Del: useless -- https://master.php.net/note/delete/107372/useless Del: bad code -- https://master.php.net/note/delete/107372/bad+code Del: spam -- https://master.php.net/note/delete/107372/spam Del: non-english -- https://master.php.net/note/delete/107372/non-english Del: in docs -- https://master.php.net/note/delete/107372/in+docs Del: other reasons-- https://master.php.net/note/delete/107372 Reject -- https://master.php.net/note/reject/107372 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185851) next »