You probably want to add the songs to an array and then call the sort()
(http://php.net/manual/en/function.sort.php) function.
change
if ($ext == "mp3" || $ext == "m4a")
{
echo "<li><a href='$item$newentry'>".basename($newentry, '.mp3')."</a></li>";
}
to
if ($ext == "mp3" || $ext == "m4a")
{
$musicarray[] = basename($newentry, '.mp3');
}
then later in your code, you can sort this mucis array
sort($musicarray);
if you get an error about the array not being defined, you might need to add
$musicarray = Array();
somewhere earlier in your code.
This will only create the array and not display it. To print the array, after your giant loop you will need to echo out evrey key from the array, something like
foreach ($musicarray as $item)
echo $item;
or equivalent
This is untested so it might need some fiddling to get working just right.