Re: usort

From: Date: Mon, 27 Oct 2003 17:31:28 +0000
Subject: Re: usort
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-167754@lists.php.net to get a copy of this message
No need to write your own function, it's already in PHP. :-) What you're looking for it natsort(). It uses natural order string comparison (which takes into account numbers instead of just using characters like a regular search does). There are also lots of other 'nat' functions, such as strnatcmp(). David Otton wrote:
On Sat, 25 Oct 2003 04:03:12 +0300, you wrote:
I don't know what the names are. I just know that there might be numbers. :) It still doesn't work. It gives very odd results with the $x_out variables.
<?php function cmp ($a, $b) { if ($a == $b) { return (0); } $a = explode ('_', $a); $b = explode ('_', $b); if ($a[0] == $b[0]) { return (((int)$a[1] < (int)$b[1]) ? -1 : 1); } return (($a[0] < $b[0]) ? -1 : 1); } $a = array('file_3.ext', 'file_10.ext', 'xxx.ext', 'file_2.ext', 'file_1.ext'); usort ($a, 'cmp'); print_r ($a); ?>


« previous php.general (#167754) next »