note 118896 added to function.uksort

From: Date: Wed, 24 Feb 2016 10:58:11 +0000
Subject: note 118896 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-205028@lists.php.net to get a copy of this message
I needed to be able to sort a string that contained numbers eg. "Slot 1 name" "Slot 2 name" "Slot 10 name" using a normal string compare the "Slot 10 name" would appear before "Slot 2 name" so I wrote little function that will compare a string taking numbers in to consideration. There may be a few edge cases that need to be taken in to consideration. function strCmpWithNumbers( $a, $b) { // Split the string in to words. $a = explode(' ',$a); $b = explode(' ',$b); $loop = 0; do { // Get the first word from each item $ta = Utils::gvfa($a, $loop); $tb = Utils::gvfa($b, $loop); if (isset($ta)) { if (isset($tb)) { if (is_numeric($ta)) { if ($ta != $tb) { return $ta - $tb; } } else { $val = strcasecmp($ta, $tb); if ($val != 0) { return $val; } } } else { return 1; // a is set but b isn't } } else { return isset($b); } $loop +=1; } while (true); } ---- Server IP: 72.52.91.14 Probable Submitter: 86.19.152.67 ---- Manual Page -- http://php.net/manual/en/function.uksort.php Edit -- https://master.php.net/note/edit/118896 Del: integrated -- https://master.php.net/note/delete/118896/integrated Del: useless -- https://master.php.net/note/delete/118896/useless Del: bad code -- https://master.php.net/note/delete/118896/bad+code Del: spam -- https://master.php.net/note/delete/118896/spam Del: non-english -- https://master.php.net/note/delete/118896/non-english Del: in docs -- https://master.php.net/note/delete/118896/in+docs Del: other reasons-- https://master.php.net/note/delete/118896 Reject -- https://master.php.net/note/reject/118896 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205028) next »