Hi all,
I'm trying to glob a directory, then sort its returned array using usort and a function I made. The desired end result is that folders would be listed alphabetically and then files alphabetically. Forget about glob for a second, and pretend I'm using this hard-coded array (that is actually the var_export of a glob heh)
$arr = array (
0 => 'C:\\wamp\\www\\config',
1 => 'C:\\wamp\\www\\controllers',
2 => 'C:\\wamp\\www\\images',
3 => 'C:\\wamp\\www\\index.php',
4 => 'C:\\wamp\\www\\integrate_this',
5 => 'C:\\wamp\\www\\models',
6 => 'C:\\wamp\\www\\scripts',
7 => 'C:\\wamp\\www\\versionCompare.php',
8 => 'C:\\wamp\\www\\views',
);
usort($arr,'sortFolder');
var_export($arr);
function sortFolder($a,$b) {
if( is_dir($a) && !is_dir($b) ) return 1;
if( !is_dir($b) && is_dir($b) ) return -1;
$a = basename($a);
$b = basename($b);
$a = str_split($a);
$b = str_split($b);
for( $i=0; $i<$len; $i++ ) {
if( ord($a[$i]) > ord($b[$i]) ) return 1;
if( ord($a[$i]) < ord($b[$i]) ) return -1;
}
return 0;
}
Which gives me the following output:
array (
0 => 'C:\\wamp\\www\\config',
1 => 'C:\\wamp\\www\\controllers',
2 => 'C:\\wamp\\www\\images',
3 => 'C:\\wamp\\www\\index.php',
4 => 'C:\\wamp\\www\\integrate_this',
5 => 'C:\\wamp\\www\\versionCompare.php',
6 => 'C:\\wamp\\www\\models',
7 => 'C:\\wamp\\www\\scripts',
8 => 'C:\\wamp\\www\\views',
)
Not entirely sure what I'm doing wrong, I'm also clueless as to why versionCompare.php moves UP the list when it should actually be last. The expected output is:
array (
0 => 'C:\\wamp\\www\\config',
1 => 'C:\\wamp\\www\\controllers',
2 => 'C:\\wamp\\www\\images',
3 => 'C:\\wamp\\www\\integrate_this',
4 => 'C:\\wamp\\www\\models',
5 => 'C:\\wamp\\www\\scripts',
6 => 'C:\\wamp\\www\\views',
7 => 'C:\\wamp\\www\\index.php',
8 => 'C:\\wamp\\www\\versionCompare.php',
)
Any ideas?