note 13186 deleted from function.natsort by philip

From: Date: Sun, 14 Jul 2002 01:16:23 +0000
Subject: note 13186 deleted from function.natsort by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-33038@lists.php.net to get a copy of this message
As you can see, natsort() will only sort on straight arrays - not maintaining index/value associations. This code fragment may help those wishing to to a natsort() on associate arrays and maintain index=>value associations: # Function called by uasort to sort # associative arrays into natural order # by the values in the array. # (c) Paul Gregg <pgregg@pgregg.com>, 2001. Function u_natsort ( $a, $b) { if ($a == $b) { return 0; } $myarr = array ($a, $b); natsort($myarr); reset($myarr); $mykey = key($myarr); if ($mykey == 0) { return -1; } else { return 1; } } uasort($sortcol, "u_natsort"); Sorry for the lack of indenting - this add note facility doesn't allow for putting html in. Paul, aka Qube.

« previous php.notes (#33038) next »