Hi,

The script below sorts mp3's in a folder after date and name. I have a problem with some files not being echoed if I try to sort decending. This happens if a change the functions below to < instead of >. I wonder why, and how I can make the script to correctly sort decending?

<style type="text/css"> a:link { text-decoration:  none; } table { border-collapse: collapse; } td { border: 1px dotted; } </style>
<?php
// Variables
$dir	= glob("*.mp3");
$sort	= $_GET['sort'];
echo "<a href=../>Back</a> | <a href=../2014>2014</a> | <b><a href=../".basename(__DIR__).">".basename(__DIR__)."</a></b></p>";

// Functions for sorting each table column title
function sort_name($a,$b) {	return $a > $b;							}
function sort_size($a,$b) {	return filesize($a) > filesize($b);		}
function sort_time($a,$b) {	return filemtime($a) > filemtime($b);	}

// Checks wich table column title is selected
if      ($sort=='name') {       usort($dir, "sort_name");  }
elseif  ($sort=='size') {       usort($dir, "sort_size");  }
elseif  ($sort=='time') {       usort($dir, "sort_time");  }
else                    {       usort($dir, "sort_time");  }

// Table
echo "<table>";
echo "<tr>";
$titles = array('nr','name','size','date','time');
foreach($titles as $title) {
	if		($title==$sort)	{ echo "<td bgcolor=lightgreen>&nbsp;<b><a href=index.php?sort=$title>".$title."</a></b>&nbsp;</td>"; }
	elseif	($title=='nr')	{ echo "<td bgcolor=lightblue>&nbsp;".ucfirst($title)."&nbsp;</td>"; }
	elseif	($title=='time'){ echo "<td bgcolor=lightblue>&nbsp;".ucfirst($title)."&nbsp;</td>"; }
	else					{ echo "<td bgcolor=lightblue>&nbsp;<a href=index.php?sort=$title>".ucfirst($title)."</a>&nbsp;</td>"; }
}
echo "</tr>";

// Table content
for($i=1; $i<count($dir); $i++) {
        echo "<tr>";
        echo "<td>&nbsp;" . $i . "&nbsp;</td>";
        echo "<td>&nbsp;<a href=" . rawurldecode($dir[$i]) . ">" . $dir[$i] . "</a>&nbsp;</td>";
        echo "<td>&nbsp;" . round(filesize($dir[$i])/(1024)/(1024),1) . " MB&nbsp;</td>";
        echo "<td>&nbsp;" . date("d.m.Y", filemtime($dir[$i])) . "&nbsp;</td>";
        echo "<td>&nbsp;" . date("H:i:s", filemtime($dir[$i])) . "&nbsp;</td>";
        echo "</tr>";
}

echo "</table><br>";
?>

    I'm not sure this will solve your problem, but shouldn't your sorting functions have something to handle if $a and $b are equal?

      Quite true: you should look at the definition of [man]usort[/man] to see what the comparison functions are supposed to return.

        Write a Reply...