How do I sort pictures alphabetically in gallery?

Hi there, I have this php gallery and I need to sort pictures alphabetically. How can I do that? Thank you.

<?php

$dir = htmlspecialchars($_GET["dir"]);
$className = htmlspecialchars($_GET["className"]);

if ($handle = opendir($dir)) {
    echo "<div class='photogallery $className'><ul>";
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
            $path = $dir."/".$entry;
            $pathThumb = $dir."/thumbs/".$entry;

            echo "
                <li>
                    <a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
                </li>";
        }
    }
    closedir($handle);
    echo "</ul><div class='both'></div></div>";
}
?>

If you use glob() the files will be sorted by default - https://www.php.net/manual/en/function.glob.php